tags:

views:

1013

answers:

1

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, 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
A: 

Of course you realize NumberField extends TextField, so there is no raw value. (wysiwyg) I suggest using a regular expression on submission.

Upper Stage
Any idea of how to hook into a form's submit action?
Mike Sickler
BasicForm has a submit() method. You could write a click handler (for your "GO" button); it will reformat the data in your NumberFields and then call submit() on your form object. Or perhaps better, the handler could grab the data from the NumberFields, clean it, and then move the data into hidden fields that are then submitted with the form. The server would ignore the dirty fields and use only the data from the cleaned fields.
Upper Stage