1

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?

Levi Hackwith
  • 9,232
  • 18
  • 64
  • 115

1 Answers1

2

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

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

take a look at src

atian25
  • 4,166
  • 8
  • 37
  • 60
  • find more details here: http://stackoverflow.com/questions/7414115/how-to-add-a-custom-validation-rule-to-a-model-in-sencha-touch – 0x6A75616E Apr 04 '12 at 04:55