1. #1
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,404
    Vote Rating
    849
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default Date Range Model validation

    Date Range Model validation


    I had a requirement to check if a start date was truly before an end date while using a pair of datefields. Instead of creating a VType or a validator for them, I wanted to use the validations on the Ext.data.Model class. Had to create two overrides to do this, one for Ext.data.Model and one for Ext.data.validations:

    Code:
    Ext.require('Ext.data.validations', function() {
        Ext.apply(Ext.data.validations, {
    
            daterangeMessage : 'Start date must be before the end date',
    
            daterange        : function(config, value, record) {
                var minValue = record.get(config.minField),
                    maxValue = record.get(config.maxField);
    
                return minValue < maxValue;
            }
        });
    });
    
    Ext.require('Ext.data.Model', function() {
        Ext.data.Model.override({
    
            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); // added 3rd argument to pass model instance
    
                        if (!valid) {
                            errors.add({
                                field  : field,
                                message: validation.message || validators[type + 'Message']
                            });
                        }
                    }
                }
    
                return errors;
            }
        });
    });
    First one is the actual validation method and message. The second is to the validate method on the Ext.data.Model class. In 4.0.7, you can only do single field validations because it doesn't pass the Model instance to the validation method. Highlighted in red is the change to accomplish this, nothing special.

    Here is the example:

    Code:
    Ext.define('DateModel', {
        extend : 'Ext.data.Model',
    
        fields : [
            { name : 'start', type : 'date' },
            { name : 'end',   type : 'date' }
        ],
    
        validations : [
            { type : 'daterange', name : 'daterange', minField : 'start', maxField : 'end' }
        ]
    });
    
    var model = Ext.create('DateModel', {
        start : new Date(),
        end   : Ext.Date.add(new Date(), Ext.Date.DAY, -1)
    });
    
    var errors = model.validate();
    This should return the errors mixedcollection with an error.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  2. #2
    Sencha Premium Member
    Join Date
    Apr 2012
    Posts
    9
    Vote Rating
    0
    srossshell is on a distinguished road

      0  

    Default Is it possible to create a custom daterangeMessage

    Is it possible to create a custom daterangeMessage


    Say for example, you have a single date field and that field can be any value between 1st march and the 1st June 2013

    So your daterangeMessage would be "The date must be between 1st Mar 2013 and the 1st Jun 2013?

  3. #3
    Sencha Premium Member
    Join Date
    Apr 2012
    Posts
    9
    Vote Rating
    0
    srossshell is on a distinguished road

      0  

    Default


    When I said custom, i meant dynamic. So that the message would be generated from the start and end values set on the Model.

  4. #4
    Sencha Premium Member
    Join Date
    Apr 2012
    Posts
    9
    Vote Rating
    0
    srossshell is on a distinguished road

      0  

    Default I think I found my answer

    I think I found my answer


    For anyone else

    http://oajamfibia.wordpress.com/2011...ons-in-extsj4/

  5. #5
    Sencha Premium Member
    Join Date
    Dec 2011
    Posts
    29
    Vote Rating
    1
    neilios is on a distinguished road

      0  

    Default


    Mitchell,

    This looks like a great way to be able to validate record values that depend on other values in the same record. I wanted exactly the same thing for dates (one must be after the other) and would like to use this.

    I was hoping that this change would make it into the main release (or an alternative) so that we can avoid having the override...
    • In v4.2.2 I see that the same code that you override here.
    • I can't seem to find it in v5.0.0 at all with just a quick look - there's no "data" folder in my src - but I've only downloaded/unpacked it for later use)
    ... do you know if there is an alternative method in 4.2.2 or 5.0.0?

    What is your recommendation - I plan to migrate to 5.0.0 as soon as I get my 4.2.2 code running.

    Thanks!

Thread Participants: 2