2016-08-17 81 views
0

如何在extjs 6.2.0现代应用程序中添加自己的表单验证器?在经典的话:extjs 6.2现代表单添加自己的验证器

validator : function(value){ 
    //do something 
}  
+0

你尝试过这么远吗? “不工作”是什么意思?你可以发布代码吗? – EJoshuaS

+0

其实我想建立一个自定义的匹配像内置 'Ext.field.Email' 类,但我不知道如何把我的手,有时我觉得现代工具包的API是缺乏 –

回答

1

这无疑取决于你想要什么,但例如我最近的确是用添加验证与视图模型绑定(似乎是在古典和现代的工具包工作)。这肯定可能会因您的需要而过于复杂,但它看起来像ViewModel与公式的绑定非常适合电子邮件验证。

形式:

Ext.define('App.login.LoginForm', { 
    extend: 'Ext.form.Panel', 

    requires: [ 'App.login.LoginModel' ], 
    viewModel: { 
     type: 'login' // references alias "viewmodel.login" 
    }, 

    layout: 'vbox', 
    defaultType: 'textfield', 
    items: [{ 
     name: 'login', 
     itemId: 'login-fld', 
     bind: '{credentials.login}' 
    },{ 
     inputType: 'password', 
     name: 'password', 
     bind: '{credentials.password}' 
    },{ 
     xtype: 'button', 
     text: 'Submit', 
     action: 'save', 
     bind: { 
      disabled: '{!isCredentialsOk}' 
     } 
    }] 
}); 

视图模型:

Ext.define("App.login.LoginViewModel", { 
    extend: "Ext.app.ViewModel", 
    alias: 'viewmodel.login', 

    links: { 
     credentials: { 
      reference: 'App.login.LoginModel', 
      create: true 
     } 
    }, 

    formulas: { 
     isCredentialsOk: function (get) { 
      return Boolean(get('credentials.login') && get('credentials.password')); 
     } 
    } 

}); 

型号:

Ext.define('App.login.LoginModel', { 
    extend: 'Ext.data.Model', 

    fields: [ 
     { name: 'login', type: 'string', allowBlank: false }, 
     { name: 'password', type: 'string', allowBlank: false } 
    ], 

    validators: [ 
     { field: 'login',  type: 'presence', message: 'Login empty' }, 
     { field: 'password', type: 'presence', message: 'Password empty' } 
    ] 
}); 
+0

感谢您的帮助,我标记它 –

+0

得到一个很好的答案,说谢谢,并且他会标记它,并且......从不接受或赞成它;( –