PDA

View Full Version : NUmericField with rawVlue have problem to save



alex_parincu
10 Apr 2012, 4:21 AM
I have a NumericField class but when I want to save the form, have a problem because the old value are in the form . Haw can save new value? The code is:
///NUMERICFIELD

Ext.ux.NumericField = function(config){
var defaultConfig =
{
style: 'text-align:right;'
};

Ext.ux.NumericField.superclass.constructor.call(this, Ext.apply(defaultConfig, config));

//Only if thousandSeparator doesn't exists is assigned when using decimalSeparator as the same as thousandSeparator
if(this.useThousandSeparator && this.decimalSeparator == ',' && Ext.isEmpty(config.thousandSeparator))
this.thousandSeparator = '.';
else
if(this.allowDecimals && this.thousandSeparator == '.' && Ext.isEmpty(config.decimalSeparator))
this.decimalSeparator = ',';

this.onFocus = this.onFocus.createSequence(this.onFocus);
};
Ext.define('Ext.ux.NumericField', {extend: 'Ext.form.NumberField', alias: 'widget.numericfield',
fieldStyle: 'text-align: right',
hideTrigger: true,
setValue: function(v){
Ext.ux.NumericField.superclass.setValue.call(this, v);
this.setRawValue(this.getFormattedValue(v));
},
getFormattedValue: function(v){
if (v == undefined || Ext.isEmpty(v)){
return v;
}
else{
return Ext.util.Format.numberRenderer('000,000,000,000,000,000,000,000,000,000,000.00')(v);
}
},
//overrides parseValue to remove the format applied by this class
parseValue: function(v){
return Ext.ux.NumericField.superclass.parseValue.call(this, this.removeFormat(v));
},
getSubmitValue: function(){
var aux = Ext.ux.NumericField.superclass.getSubmitValue.call(this);
return this.removeFormat(this.rawValue);
},
//Remove only the format added by this class to let the superclass validate with it's rules.
removeFormat: function(v){
if ( v == undefined || Ext.isEmpty(v)){
return v;
}else{
if(v != undefined){
v = v+"";
while(v.indexOf(Ext.util.Format.thousandSeparator)!=-1){
v = v.replace(Ext.util.Format.thousandSeparator, '');
}
v = v.replace(Ext.util.Format.decimalSeparator, '.');
}
return v;
}
},
removeDecimals: function(v){
idx = v.indexOf(Ext.util.Format.decimalSeparator);
if(idx!=-1){
return v.substring(0,idx);
}
return v;
},
/**
* Display the numeric value with the fixed decimal precision and without the format using the setRawValue, don't need to do a setValue because we don't want a double
* formatting and process of the value because beforeBlur perform a getRawValue and then a setValue.
*/
onFocus: function(){
this.setRawValue(this.removeFormat(this.rawValue));
},
onBlur: function(){
Ext.ux.NumericField.superclass.onBlur.call(this);
this.setValue(this.getFormattedValue(this.getValue()));
},
getErrors: function(value) {
var me = this,
errors = me.callParent(arguments),
format = Ext.String.format,
num;
value = Ext.isDefined(value) ? value : this.processRawValue(this.getRawValue());
if (value == null || value.length < 1) {
return errors;
}
value = this.removeFormat(value);
if(isNaN(value)){
errors.push(format(me.nanText, value));
}
num = me.parseValue(value);
if (me.minValue === 0 && num < 0) {
errors.push(this.negativeText);
}
else if (num < me.minValue) {
errors.push(format(me.minText, me.minValue));
}
if (num > me.maxValue) {
errors.push(format(me.maxText, me.maxValue));
}
return errors;
},
isValid : function() {
var me = this,
disabled = me.disabled,
validate = me.forceValidation || !disabled;
return validate ? me.validateValue(me.processRawValue(this.removeFormat(this.removeDecimals(this.rawValue)))) : disabled;
},

});