extjs form model vilidations

11858 단어 formModelExtJsvalid
CODE1:

       Ext.override(Ext.form.field.Base, {
                bindToModel : false,
                
                getErrors: function() {
                    var errors = this.callOverridden(arguments),
                        record = this.up('form').getRecord(),                        
                          modelValidations, 
                          fieldValidations;    
                    
                      if (!this.bindToModel || !record) return errors;
                      
                      modelValidations = record.validate();
                      if (modelValidations.isValid()) {
                          return errors;
                      }
                      
                      fieldValidations = modelValidations.getByField(this.name);
                      fieldValidationsLength = fieldValidations.length;
                      
                      if (fieldValidationsLength > 0) {
                          for (var j = 0; j < fieldValidationsLength; j++) {
                              errors.push(fieldValidations[j].message);
                          }                      
                      }
                      
                      return errors;
                },
                
                onChange: function() {
                    var form = this.up('form').getForm(),
                        record = form.getRecord();
                    
                    if (this.bindToModel && record) {
                        form.updateRecord(record);
                    }
                    
                    this.callOverridden(arguments);
                }
            });
            
            Ext.define('Workgroup', {
                extend: 'Ext.data.Model',
                fields: [
                    'name',
                    {name: 'startDate', type: 'date'},
                    {name: 'endDate', type: 'date'}
                ],
                validations: [
                    {type: 'presence', name: 'name', message: 'Name is required.'},
                    {type: 'length', name: 'name', min: 3, message: 'Names must be at least 3 characters in length.'},
                    {type: 'presence', name: 'startDate', message: 'Start Date is required.'}                    
                ]
            });            

            var formPanel = Ext.create('Ext.form.Panel', {
                frame: true,
                title: 'Work Group',
                width: 340,
                bodyPadding: 5,
                renderTo: Ext.getBody(),
                fieldDefaults: {
                    anchor: '100%',               
                    bindToModel: true
                },

                items: [
                    {
                        xtype: 'textfield',
                        name: 'name',
                        fieldLabel: 'Name'
                    },
                    {
                        xtype: 'datefield',
                        name: 'startDate',
                        fieldLabel: 'Start Date'
                    }
                ],
                
                buttons: [
                    {
                        text: 'Check',
                           handler: function() {    
                               console.log('Record data: ', workgroup.data);
                               console.log('Form record data: ', formPanel.getRecord().data);
                            console.log('Valid: ', formPanel.getForm().isValid());
                        }
                    }
                ]
            });
            
            var workgroup = Ext.create('Workgroup', {
                name: 'R12345'
            });
            
            formPanel.loadRecord(workgroup);

 
2:this.callOverridden(arguments); 원시 프로그램 호출 방법
삼:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Form <-> Model bind</title>
    <link rel="stylesheet" type="text/css" href="http://extjs.cachefly.net/ext-4.0.2a/resources/css/ext-all.css"/>
    <script type="text/javascript" src=\'#\'" //extjs.cachefly.net/ext-4.0.2a/ext-all-debug.js"></script>
    <script>
    
    Ext.override(Ext.form.Basic, {
        loadRecord: function(record) {
            var me = this;
            me._validationRecord = record.copy();
            me.callOverridden(arguments);
            me.mon( record, 'changed', me.onRecordChanged, me );
        },
        onRecordChanged: function(){
            this.loadRecord( this.getRecord() );
        }
    });
    Ext.override(Ext.form.field.Base, {
        getErrors: function() {
            var me = this,
                errors = me.callOverridden(arguments),
                record = me.up('form').getForm()._validationRecord,                        
                modelValidations, 
                fieldValidations;    
            
              if (!me.bindToModel || !record) return errors;
              
              modelValidations = record.validate();
              if (modelValidations.isValid()) {
                  return errors;
              }
              
              fieldValidations = modelValidations.getByField(me.name);
              fieldValidationsLength = fieldValidations.length;
              
              if (fieldValidationsLength > 0) {
                  for (var j = 0; j < fieldValidationsLength; j++) {
                      errors.push(fieldValidations[j].message);
                  }                      
              }
              
              return errors;
        },
        
        onChange: function() {
            var me = this,
            form = me.up('form').getForm(),
            record = form._validationRecord;


            if (me.bindToModel && record && (me.name in record.data)) {
                record.beginEdit();
                record.set(me.name, me.getValue());
                record.endEdit();
            }


            this.callOverridden(arguments);
        }
        
    });
                
    Ext.define('Workgroup', {
        extend: 'Ext.data.Model',
        fields: [
            'name',
            {name: 'startDate', type: 'date'},
            {name: 'endDate', type: 'date'}
        ],
        validations: [
            {type: 'presence', name: 'name', message: 'Name is required.'},
            {type: 'length', name: 'name', min: 3, message: 'Names must be at least 3 characters in length.'},
            {type: 'presence', name: 'startDate', message: 'Start Date is required.'}                    
        ],
        afterEdit: function() {
            
            var me = this;
            if(me.dirty){
                me.fireEvent('changed', me);
            }
            this.callParent(arguments);
        } 
    });            


    Ext.onReady(function() {
        var formPanel = Ext.create('Ext.form.Panel', {
            frame: true,
            title: 'Work Group',
            width: 340,
            bodyPadding: 5,
            renderTo: Ext.getBody(),
            
            fieldDefaults: {
                anchor: '100%',               
                bindToModel: true
            },
            


            items: [
                {
                    xtype: 'textfield',
                    name: 'name',
                    fieldLabel: 'Name'
                },
                {
                    xtype: 'datefield',
                    name: 'startDate',
                    fieldLabel: 'Start Date'
                }
            ],
            
            buttons: [
                {
                    text: 'Check',
                       handler: function() {    
                            console.log('Record data: ', workgroup.data);
                            console.log('Form record data: ', formPanel.getRecord().data);
                            console.log('Form validation record data: ', formPanel.getForm()._validationRecord.data);
                            console.log('Valid: ', formPanel.getForm().isValid());
                    }
                }
            ]
        });


        var workgroup = Ext.create('Workgroup', {
            name: 'R12345'
        });


        formPanel.loadRecord(workgroup);
        workgroup.set('name', 'New Name');
    });


</script>
</head>
<body></body>
</html>

사:

errors.isValid(); //false errors.length; //2 errors.getByField('name'); // [{field: 'name', error: 'must be present'}] errors.getByField('title'); // [{field: 'title', error: 'is too short'}]

오:

    /**
     * Persists the values in this form into the passed {@link Ext.data.Model} object in a beginEdit/endEdit block.
     * @param {Ext.data.Record} record The record to edit
     * @return {Ext.form.Basic} this
     */
    updateRecord: function(record) {
        //record.beginEdit();
        var fields = record.fields,
            values = this.getValues(),
            name,
            obj = {};
        fields.each(function(f) {
            name = f.name;
            if (name in values) {
                obj[name] = values[name];
            }
        });
        record.set(obj);
        //record.endEdit();
        return this;
    },

육:

var errors = myModel.validate();

errors.isValid(); //false

errors.length; //2
errors.getByField('name');  // [{field: 'name',  error: 'must be present'}]
errors.getByField('title'); // [{field: 'title', error: 'is too short'}]

 
 
 
 

좋은 웹페이지 즐겨찾기