【问题标题】:ExtJs dependent field validationExtJs 依赖字段验证
【发布时间】:2011-03-03 21:10:54
【问题描述】:

如何验证一个依赖于另一个字段的字段?

{
  xtype:          'textfield',
  name:           'name2',
  vtype:          'type',      // how to write the validation code for this if it 
                               // depends on the value of another field?
  allowBlank:     false
}

【问题讨论】:

    标签: javascript validation extjs


    【解决方案1】:

    通过添加您自己的自定义验证器并在其中执行您的验证。

    var field_one = new Ext.form.TextField({
        name: 'field_one',
        fieldLabel: 'Field one'
    });
    
    var field_two = new Ext.form.TextField({
        name: 'field_two',
        fieldLabel: 'Field two',
        validator: function(value){
            if(field_one.getValue() != value) {
                return 'Error! Value not identical to field one';
            } else {
                return true;
            }
        }
    });
    

    【讨论】:

    • 很好的答案,但是如何在另一个地方执行验证,例如在另一个按钮的动作监听器内。
    • 这也是可能的,但如果您在按钮处理程序中,则必须通过范围或Ext.getCmp() 获取对元素的引用。然后执行验证并手动将字段标记为无效field.markInvalid('...')
    • 关于@ChrisR 关于获得参考的评论。您的字段可能在同一个容器中。因此,您可以轻松地获得对验证器函数内其他字段的引用。函数内部的this 是配置验证器的文本字段。使用:var otherField = this.up().down('field[name=field_one]') (ExtJS 4) 获取对其他字段的引用
    【解决方案2】:

    字段定义:

    ....
    monitorValid:     true,
    ....
    }, {
      xtype:          'textfield',
      name:           'name1',
      ref:            'name1',
    
    }, {
      xtype:          'textfield',
      name:           'name2',
      ref:            'name2',
      allowBlank:     false,
    ....
    

    initComponent 中的下一个(如果您愿意,也可以是监听器):

    this.name2.on ( 'change', this._validate_name2, this );
    

    并在 FormPanel 中定义处理程序:

    this._validate_name2: function ( ) {
       if ( this.name1.getValue () == this.name2.getValue () ) {
          this.name2.markInvalid ( 'field does not match name1' );
          this.name2.setValue ( null );
       }
    }
    

    "markInvalid() 方法不会导致 Field 的 validate 方法在值通过验证时返回 false。因此,简单地将 Field 标记为无效不会阻止提交使用 Ext.form.Action.Submit.clientValidation 提交的表单选项集。”

    因此组合 allowBlank 和 setValue ( null ) 会破坏验证

    【讨论】:

      【解决方案3】:

      我模拟了一个示例,说明我如何在 Ext JS 5.1 中使用组合框... .这是代码(和Fiddle):

      Ext.application({
        name: 'Fiddle',
      
        launch: function() {
          Ext.define('MyComboViewController', {
            extend: 'Ext.app.ViewController',
            alias: 'controller.mycombo',
            init: function() {
              this.getView().setStore(this.createStore());
            },
            createStore: function() {
              var store = Ext.create('Ext.data.Store', {
                fields: [
                  {name: 'disp', type: 'string'},
                  {name: 'val', type: 'int'}
                ],
                data: [
                  {disp: 'One', val: 1},
                  {disp: 'Two', val: 2},
                  {disp: 'Three', val: 3},
                  {disp: 'Four', val: 4},
                  {disp: 'Five', val: 5}
                ],
                proxy: {
                  type: 'memory'
                }
              });
              return store;
            }
          });
      
          Ext.define('MyCombo', {
            extend: 'Ext.form.field.ComboBox',
            xtype: 'myCombo',
            controller: 'mycombo',
            displayField: 'disp',
            valueField: 'val',
            labelAlign: 'top',
            validateOnChange: false,
            typeAhead: true,
            queryMode: 'local'
          });
      
          Ext.define('MyCombosContainerViewController', {
            extend: 'Ext.app.ViewController',
            alias: 'controller.mycomboscontainer',
            init: function() {
              var startCombo = this.lookupReference('startCombo');
              var endCombo = this.lookupReference('endCombo');
              startCombo.validator = Ext.bind(this.comboValidator, this, [startCombo, endCombo]);
              endCombo.validator = Ext.bind(this.comboValidator, this, [startCombo, endCombo]);
            },
            comboValidator: function(startCombo, endCombo) {
              return startCombo.getValue() < endCombo.getValue();
            },
            onSelectComboBox: function(combo) {
              var startCombo = this.lookupReference('startCombo');
              var endCombo = this.lookupReference('endCombo');
              startCombo.validate();
              endCombo.validate();
            }
          });
      
          Ext.define('MyCombosContainer', {
            extend: 'Ext.form.FieldContainer',
            controller: 'mycomboscontainer',
            layout: {
              type: 'hbox',
              align: 'stretch'
            },
            items: [{
              xtype: 'myCombo',
              reference: 'startCombo',
              fieldLabel: 'Start',
              listeners: {
                select: 'onSelectComboBox'
              }
            }, {
              xtype: 'myCombo',
              reference: 'endCombo',
              fieldLabel: 'End',
              listeners: {
                select: 'onSelectComboBox'
              }
            }]
          });
      
          Ext.create('MyCombosContainer', {
            renderTo: Ext.getBody()
          });
        }
      });
      

      【讨论】:

      • 次要注意:通常类是在调用Ext.application之外定义的
      【解决方案4】:

      为了验证链接字段,我通常会创建一个函数(我将其添加到我的 Ext.lib.Validators 类中,以便我可以在整个应用程序中调用它)返回一个具有预配置范围和验证逻辑的匿名函数(因此我可以多次使用它我的申请)。

      这是一个例子:

      myValidator: function (firstFieldSelector, secondFieldSelector, thirdFieldSelector) {
          return function () {
              var firstField = Ext.ComponentQuery.query(firstFieldSelector)[0],
                  secondField= Ext.ComponentQuery.query(secondFieldSelector)[0],
                  thirdField= Ext.ComponentQuery.query(thirdFieldSelector)[0];
      
              if (firstField && secondField && thirdField) {
                  // Validation logic here...
                  if( true ) {
                      return true;
                  } else {
                      return 'Error text here...';
                  }
              } else {
                  // Validator incorrectly configured, do not validate with it
                  return true;
              }
          }
      }
      

      这里是一个example fiddle,带有时间跨度选择。

      【讨论】:

        【解决方案5】:

        一般来说 - 我建议在所有需要交叉验证的字段上连接更改事件侦听器。在更改事件处理程序中,我们需要在需要对正在修改的字段进行验证的每个其他字段上触发验证。当您有一个表单并且有很多字段并且需要进行大量验证时,这种方法非常有效。

        【讨论】:

          猜你喜欢
          • 2016-05-27
          • 2011-04-27
          • 2011-01-01
          • 1970-01-01
          • 2013-09-12
          • 2020-04-17
          • 2012-09-13
          • 2016-01-10
          • 1970-01-01
          相关资源
          最近更新 更多