1
votes

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

1 Answers

2
votes

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

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

take a look at src