Home > extjs4 > Cross field model validation in extjs4

Cross field model validation in extjs4

I dont see an easy way to make validations where one field depends on another. I think they must have forgotten to support it. What is missing is the model instance passed to the validation method. Now it looks like this:
    function(config, value)
and we would like it to look like this
    function(config, value, model)
, then we would have access to all fields.

But we just need to override Ext.data.Model.validate() and pass the model instance to the validation function as an extra parameter:

valid = validators[type](validation, this.get(field), this);

Ext.override(Ext.data.Model, {
    validate: function() {
        var errors = Ext.create('Ext.data.Errors'),
                validations = this.validations,
                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;
                valid = validators[type](validation, this.get(field), this);

                if (!valid) {
                    errors.add({
                        field  : field,
                        message: validation.message || validators[type + 'Message']
                    });
                }
            }
        }

        return errors;
    }
});

Now we have access to the model instance:

Ext.data.validations.range = function(config, value, model) {
    ....
    var someValue = model.get('someField')
    ....
}
Advertisements
  1. Edgar A. Miranda
    July 17, 2013 at 11:18

    Una solución similar en caso de que la validación se deba realizar conforme a dos campos

    En el modelo:
    validations[
    {type:”, field:”, field2:”},
    {type:”, field:”}
    ]

    Sobrecarga de Ext.data.Model

    Ext.override(Ext.data.Model,{
    validate: function() {
    var errors= new Ext.data.Errors();
    var validations = this.validations;
    var validators = Ext.data.validations;
    var length, validation, field, field2, valid, type, i;

    console.log(‘prueba1’);
    if (validations) {
    length = validations.length;

    for (i = 0; i < length; i++) {
    validation = validations[i];
    field = validation.field || validation.name;
    field2= validation.field2;
    type = validation.type;

    //Permite validar por un sólo campo field o por dos campos definiendo field2 en el modelo
    if(!field2) valid = validators[type](validation, this.get(field));
    else valid= validators[type](validation, this.get(field), this.get(field2));

    if (!valid) {
    errors.add({
    field : field,
    field2: field2,
    message: validation.message || validators[type + 'Message']
    });
    }
    }
    }

    return errors;
    }
    });

    Saludos

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: