ExtJS Model Validations: Dates (How to)

匿名 (未验证) 提交于 2019-12-03 01:33:01

问题:

What's the best way to go about adding validation to a date field in a model

Ext.define('User', {     extend: 'Ext.data.Model',     fields: [         {name: 'name',     type: 'string'},         {name: 'age',      type: 'int'},         {name: 'phone',    type: 'string'},         {name: 'gender',   type: 'string'},         {name: 'username', type: 'string'},         {name: 'alive',    type: 'boolean', defaultValue: true}     ],      validations: [         {type: 'presence',  field: 'age'},         {type: 'length',    field: 'name',     min: 2},         {type: 'inclusion', field: 'gender',   list: ['Male', 'Female']},         {type: 'exclusion', field: 'username', list: ['Admin', 'Operator']},         {type: 'format',    field: 'username', matcher: /([a-z]+)[0-9]{2,3}/}     ] });

Let's say the above code contained a 'dob' field for date of birth. How would I go about adding a validation for it?

My assumption is that I would use :

{type: 'format', field: 'dob', matcher: /([a-z]+)[0-9]{2,3}/}

but would use a regex that's designed to validate a date. Is there a better way to do this? I noticed that date fields on forms use their own validation methods to highlight a date field. Is there something like that for date fields in models?

回答1:

add validate method to Ext.data.validations (singleton),

and it will be use at Ext.data.Model.validate().

take a look at src



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!