How can I control the format of a Ext.form.NumberField value when a form submits?
I've extended Ext.form.Numberfield to show thousands separators and always show two decimal places:
Ext.override(Ext.form.NumberField, {
baseChars: "0123456789,",
setValue: function(v){
v = typeof v == 'number' ? v : String(v).replace(this.decimalSeparator, ".").replace(/,/g, "");
//v = isNaN(v) ? '' : String(v).replace(".", this.decimalSeparator);
v = isNaN(v) ? '' : Ext.util.Format.number(this.fixPrecision(String(v)), "0,000,000.00");
this.setRawValue(v);
return Ext.form.NumberField.superclass.setValue.call(this, v);
},
fixPrecision: function(value){
var nan = isNaN(value);
if (!this.allowDecimals || this.decimalPrecision == -1 || nan || !value) {
return nan ? '' : value;
}
return parseFloat(value).toFixed(this.decimalPrecision);
},
validateValue: function(value){
if (!Ext.form.NumberField.superclass.validateValue.call(this, value)) {
return false;
}
if (value.length < 1) { // if it's blank and textfield didn't flag it then it's valid
return true;
}
value = String(value).replace(this.decimalSeparator, ".").replace(/,/g, "");
if (isNaN(value)) {
this.markInvalid(String.format(this.nanText开发者_Go百科, value));
return false;
}
var num = this.parseValue(value);
if (num < this.minValue) {
this.markInvalid(String.format(this.minText, this.minValue));
return false;
}
if (num > this.maxValue) {
this.markInvalid(String.format(this.maxText, this.maxValue));
return false;
}
return true;
},
parseValue: function(value){
value = parseFloat(String(value).replace(this.decimalSeparator, ".").replace(/,/g, ""));
return isNaN(value) ? '' : value;
}
});
The problem is that on form submit, the value sent in the POST includes the commas, forcing me to parse as a string on the server side. Is there a way to send the raw number value instead of this special comma-formatted value?
Instead of sending these parameters:
referenceSales 10,000,000.00
salesGoal 11,000,000.00
I want to send these:
referenceSales 10000000.00
salesGoal 11000000.00
Of course you realize NumberField extends TextField, so there is no raw value. (wysiwyg) I suggest using a regular expression on submission.
精彩评论