View Full Version : Date Range Model validation

9 Nov 2011, 1:04 PM
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:

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() {

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) {
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:

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.

28 May 2013, 3:16 AM
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?

28 May 2013, 3:17 AM
When I said custom, i meant dynamic. So that the message would be generated from the start and end values set on the Model.

28 May 2013, 3:24 AM
For anyone else


7 Jun 2014, 8:13 AM

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.