Home > extjs4 > Making custom model validations in extjs4.

Making custom model validations in extjs4.

Adding a custom validation to the Ext.data.validations is quite simple.

The are a few validations already, but not one that validates, that a number value lies within a minimum and maximum. I will call the new validation for range

Ext.data.validations is a singleton and the new range validation is installed by adding an error message and a validation function:

Ext.data.validations.rangeMessage = 'Value must be between {0} and {1}' ;

Ext.data.validations.range = function(config, value) {
   if (value === undefined) {
      return false;
   var min = config.min,
   max = config.max;
   if ((value < min) || (value > max)) {
      config.message = Ext.String.format(this.rangeMessage, min, max);
      return false;
   } else {
      return true;

The validation is configured like this in the validations of the model:

   field: 'price',
   type: 'range',
   min: 5,
   max: 100

This is also the config parameter of the validation function and the value parameter is the value of the field. So we have a pricfield where the number has to be between 5 and 100.

This range validation is a bit more complicated than the other validations, because it has an error message with parameters and it is calculated in the validation function. The calculated message is stored in config.message. When the model is validated the model.validate() method first checks for a config.message, then for Ext.data.validations.rangeMessage.

  1. September 6, 2011 at 04:55

    Thanks dude! Really helped 🙂

  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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: