Last active
November 20, 2015 09:27
-
-
Save mcelotti/fa7c16112735d36995e9 to your computer and use it in GitHub Desktop.
Sencha Touch 2.4.2 override validate() to pass model and allow cross-field validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ext.define('MyApp.override.data.Model', { | |
override : 'Ext.data.Model', | |
/** | |
* @override | |
* we want to pass the model (not only config and value) | |
*/ | |
validate : function() { | |
var errors = Ext.create('Ext.data.Errors'), | |
validations = this.getValidations().items, | |
validators = Ext.data.Validations, | |
length, | |
validation, | |
field, | |
valid, | |
type, | |
i; | |
if (validations) { | |
length = validations.length; | |
for ( i = 0; i < length; i++) { | |
validation = validations[i]; | |
field = validation.field || validation.name; | |
type = validation.type; | |
// here we go with the model | |
valid = validators[type](validation, this.get(field), this); | |
if (!valid) { | |
errors.add(Ext.create('Ext.data.Error', { | |
field : field, | |
message : validation.message || validators.getMessage(type) | |
})); | |
} | |
} | |
} | |
return errors; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why
This override is useful if you need to do some cross-field validations (ie a field is valid only if another field has a valid value)
Usage
First add this custom validator:
Then add to model: