PDA

View Full Version : How model validation can be done?



Mahmoud690
11 May 2014, 2:28 AM
In ExtJs 4.2 I had the following codes:

Ext.define('SignInModel', {
extend: 'Ext.data.Model',
fields: [
{ name: 'Username' },
{ name: 'Password' },
{ name: 'grant_type', defaultValue: 'password' }
],
validations: [
{ type: 'presence', name: 'Username' },
{ type: 'presence', name: 'Password' },
],


});

But these validations don't work inside ExtJs 5.0. How can I solve it?!

Carun
12 May 2014, 5:31 AM
As per the upgrade guide (http://docs.sencha.com/ext/5.0.0/whats_new/5.0/extjs_upgrade_guide.html#Models) and the API documentation example, use the validators config instead of validations.


validations: Supported but deprecated for creating validators associated with a single field. For these use the validators config object which is keyed by field name.






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 }
],


validators: {
age: 'presence',
name: { type: 'length', min: 2 },
gender: { type: 'inclusion', list: ['Male', 'Female'] },
username: [
{ type: 'exclusion', list: ['Admin', 'Operator'] },
{ type: 'format', matcher: /([a-z]+)[0-9]{2,3}/i }
]
}
});