-
11 Apr 2012 3:45 AM #1
Answered: Model cross-field Validation, (in a Store)
Answered: Model cross-field Validation, (in a Store)
Hi!
I would like to do model cross-fields validation.
I read this thread
http://stackoverflow.com/questions/7...276996#9276996
proposing to add a reference to the model in each validation item:
Then the idea is to have a model (MyModel) extending CustomModel.Code:Ext.define('MyApp.model.CustomModel', { extend: 'Ext.data.Model', constructor: function() { this.callParent(arguments) var i, len; if (this.config.validations) { for (i = 0, len = this.config.validations.length; i < len; i++) { if (this.config.validations[i].self) { console.log(this.config.validations[i].self) } this.config.validations[i].self = this; } } } });
When I use a single instance of MyModel, everything is fine.
But as soon as I define a Store of MyModel, I see that the 'this' object is not a new instance of the model for each record of the store. (see the console.log... )
Is it correct that all the records of a store share a unique model instance ?
If this is true, how can I deal with the problem of providing a reference of each record to the validation ?
Thanks
-
Best Answer Posted by snamI found this solution (in the Sencha Touch 1 forum, but it worked for me in ST2):http://www.sencha.com/forum/showthre...ds-validationsIt requires an override of the validate() method of the Model class, but if you don't mind then this is easier than what I exposed earlier...
-
11 Apr 2012 4:23 AM #2
I think I identified the problem.
In Model.js, in the 'onClassExtended' function (which I guess is called when I define the 'CustomModel' class):
This code is a bit tricky for me to understand, but if I get it right, when a new instance of Model is created, if the 'validations' field already exists in the superclass, then it is cloned instead of creating a new one.Code:/** * @property {Ext.util.Collection} validations * The validations defined on this model. */ cls.prototype.validations = cls.validations = cls.prototype._validations = (superCls && superCls.validations) ? superCls.validations.clone() : new Ext.util.Collection(function(validation) { return validation.field || validation.name; });
Then I guess the idea proposed in the previous post cannot work when used with Stores ?
Any other way to do cross-field model validation ?
Thanks
-
12 Apr 2012 6:01 AM #3
Found a solution...
Found a solution...
I found this solution (in the Sencha Touch 1 forum, but it worked for me in ST2):http://www.sencha.com/forum/showthre...ds-validationsIt requires an override of the validate() method of the Model class, but if you don't mind then this is easier than what I exposed earlier...


Reply With Quote