ExtJs依赖字段验证

fas*_*ava 11 javascript validation extjs

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

{
  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
}
Run Code Online (Sandbox Code Playgroud)

Chr*_*isR 25

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

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;
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

  • 这也是可能的,但如果你在按钮处理程序中,你必须通过范围或`Ext.getCmp()`来引用你的元素.然后执行验证并使用`field.markInvalid('...')`手动将字段标记为无效 (2认同)