PDA

View Full Version : Models don't know how to validate numeric types



c089
23 Apr 2011, 6:51 AM
Not a bug, more of a missing feature: All the Ext.data.Validations validtors are string-centric or the documentation doesn't mention anything on numeric validations (like "this 'price' float should not be less than 0.01" or "this 'rgb_red' integer should be between 0 and 255").

Also, theres a totally unreleated bug in the forums: the preview button always submits ;)

Chris

c2c-guinness
25 Apr 2011, 6:00 PM
+1

lacco
16 Feb 2012, 1:43 AM
Perhaps we should add this functionality on our own... To begin:



Ext.apply(Ext.data.validations, {
numericality: function(config, value){
value = parseFloat(value);

var isNaN = value == NaN;
console.info(config);

if(("greater_than" in config) && (isNaN || value <= config.greater_than)){
return false;
} else if(("greater_than_or_equal_to" in config) && (isNaN || value < config.greater_than_or_equal_to)){
return false;
}

if(("less_than" in config) && (isNaN || value >= config.less_than)){
return false;
} else if(("less_than_or_equal_to" in config) && (isNaN || value > config.less_than_or_equal_to)){
return false;
}

return true;
}
});


Usage:


{type: 'numericality', field: 'my_field', greater_than: 0, less_than_or_equal: 255}