1. #1
    Sencha User
    Join Date
    Mar 2012
    Posts
    28
    Answers
    2
    Vote Rating
    0
    snam is on a distinguished road

      0  

    Default 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:
    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;
                }
            }
        }
    });
    Then the idea is to have a model (MyModel) extending CustomModel.

    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

  2. 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...

  3. #2
    Sencha User
    Join Date
    Mar 2012
    Posts
    28
    Answers
    2
    Vote Rating
    0
    snam is on a distinguished road

      0  

    Default


    I think I identified the problem.
    In Model.js, in the 'onClassExtended' function (which I guess is called when I define the 'CustomModel' class):

    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;
         });
    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.

    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

  4. #3
    Sencha User
    Join Date
    Mar 2012
    Posts
    28
    Answers
    2
    Vote Rating
    0
    snam is on a distinguished road

      0  

    Default 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...

Tags for this Thread