ExtJS模型验证:日期(方法) [英] ExtJS Model Validations: Dates (How to)
问题描述
向模型中的日期字段添加验证的最佳方法是什么
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?
我的假设是我会使用:
{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?
推荐答案
将validate方法添加到Ext.data.validations(单例)中,
add validate method to Ext.data.validations (singleton),
,它将在Ext.data.Model.validate()中使用.
and it will be use at Ext.data.Model.validate().
看看src
这篇关于ExtJS模型验证:日期(方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!