mitchellsimoens
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() {
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:
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.
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:
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.