Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Model conditional validations

  1. #1
    Sencha Premium Member
    Join Date
    Feb 2012
    Posts
    40
    Answers
    2

    Default Model conditional validations

    Hi,

    I'm looking for a way to create conditional validations on my models.
    For example I'll need to validate field A only if field B is true.
    According to the documentation on `Ext.data.validator.Validator` that should be possible because the validate function passed the record, but in reality the record is not passed on the record validate nor the form validation (using modelValidation).

    So how can we create conditional validations?

  2. #2
    Sencha - Community Support Team jsakalos's Avatar
    Join Date
    Apr 2007
    Location
    Slovakia
    Posts
    27,664
    Answers
    512

    Default

    If the record is not passed and the documentation says it is, then it is a bug. Make please a showcase at https://fiddle.sencha.com and report it as bug.
    Jozef Sakalos, aka Saki

    Education, extensions and services for developers at new http://extjs.eu
    News: Grid MultiSearch Plugin, Grid MultiSort Plugin, Configuring ViewModel Hierarchy


  3. #3
    Sencha Premium Member
    Join Date
    Feb 2012
    Posts
    40
    Answers
    2

    Default

    You can see the comment here:

    result = validator.validate(value); // we have no record to pass

    and the documentation here.


    I'm still looking for a way to add conditional validations into my models.


    (@jsakalos:I suppose you'll be able to report it as bug if you'd like.)

  4. #4
    Sencha Premium Member
    Join Date
    Jul 2014
    Posts
    70
    Answers
    1

    Default

    I am also looking for a solution to this. Any ideas?

  5. #5
    Sencha Premium Member
    Join Date
    Feb 2012
    Posts
    40
    Answers
    2

    Default I've got an hacky workaround

    Quote Originally Posted by lmastrodicasa View Post
    I am also looking for a solution to this. Any ideas?
    Since Sencha's support is so great I made up this workaround:

    1. The model constructor:

    Code:
    constructor: function (config) {
        this.callParent(arguments);
    
    Ext.Array.each(this.getFields(), function (field) {
            field.record = this;
    }, this);
    }
    2. Override the validate method in Ext.data.field.Field:

    Code:
    Ext.define('MyApp.data.field.Field', {
            override: 'Ext.data.field.Field',
    
    validate: function (value, separator, errors) {
                var me = this,
    ret = '',
    result, validator, validators, length, i;
    if (!me._validators) {
                    me.compileValidators();
    }
                validators = me._validators;
    for (i = 0 , length = validators.length; i < length; ++i) {
                    validator = validators[i];
    result = validator.validate(value, this.record);
    
    if (result !== true) {
                        result = result || me.defaultInvalidMessage;
    if (errors) {
                            errors.add(me.name, result);
    ret = ret || result;
    } else if (separator) {
                            if (ret) {
                                ret += separator;
    }
                            ret += result;
    } else {
                            ret = result;
    break;
    }
                    }
                }
                return ret || true;
    }
    
        }
    );

  6. #6
    Sencha Premium User bobringer's Avatar
    Join Date
    Apr 2007
    Location
    New Jersey
    Posts
    88
    Answers
    1

    Default

    Has a bug report been filed for this, or is it just a question in Q&A?

  7. #7
    Sencha Premium User bobringer's Avatar
    Join Date
    Apr 2007
    Location
    New Jersey
    Posts
    88
    Answers
    1

    Default

    Also... this is the right answer. Plug this into your overrides...

    Thanks for this. Saved me an hour or so, and at midnight, that makes me happy.

    Quote Originally Posted by chanand View Post
    Since Sencha's support is so great I made up this workaround:

    1. The model constructor:

    Code:
    constructor: function (config) {
        this.callParent(arguments);
    
    Ext.Array.each(this.getFields(), function (field) {
            field.record = this;
    }, this);
    }
    2. Override the validate method in Ext.data.field.Field:

    Code:
    Ext.define('MyApp.data.field.Field', {
            override: 'Ext.data.field.Field',
    
    validate: function (value, separator, errors) {
                var me = this,
    ret = '',
    result, validator, validators, length, i;
    if (!me._validators) {
                    me.compileValidators();
    }
                validators = me._validators;
    for (i = 0 , length = validators.length; i < length; ++i) {
                    validator = validators[i];
    result = validator.validate(value, this.record);
    
    if (result !== true) {
                        result = result || me.defaultInvalidMessage;
    if (errors) {
                            errors.add(me.name, result);
    ret = ret || result;
    } else if (separator) {
                            if (ret) {
                                ret += separator;
    }
                            ret += result;
    } else {
                            ret = result;
    break;
    }
                    }
                }
                return ret || true;
    }
    
        }
    );

  8. #8
    Sencha User
    Join Date
    Feb 2013
    Location
    California
    Posts
    11,985
    Answers
    506

    Default

    I don't believe a bug has been filed for this. Can you please post a test case which exhibits the behavior?
    https://fiddle.sencha.com/#home

  9. #9
    Sencha Premium Member
    Join Date
    Apr 2013
    Location
    NYC
    Posts
    47
    Answers
    2

    Exclamation

    I dont see how that override will work.

    The model fields are held at the class level (in the inheritableStatics section), so 2 instances of the same model share the same field objects. Thus, in all instances of the model, the fields will point to the last record created.

    You could do something like override Ext.data.Validation.refresh() to pass the record into the field, which in turn would have to pass the record down to the validator...
    Not very pretty either way...

  10. #10

    Default

    Just spent good hours trying to figure this out. Is there any good workaround?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •