-
20 Dec 2012 7:50 PM #1
binding a Checkbox to a numeric field
binding a Checkbox to a numeric field
Hello:
I have a model with an int field called HasNotes, which is 1 or 0 and this feeds a form CheckBox.
When I call the form updateRecord, HasNotes is set to NaN because updateRecord feeds the model the CheckBox value, which is true or false.
Is it possible to intercept the value and save in the store 1 or 0 instead? If yes, how?
Thanks!
-
21 Dec 2012 2:16 AM #2
Have you tried the "inputValue"/"uncheckedValue" properties of Ext.form.field.Checkbox?
-
21 Dec 2012 10:12 AM #3
I actually did but I forgot to mention it in the post.
I also looked at the source code for the Checkbox component. The problem is that when the data is moved from the form to the record (or model instance, if you want) ExtJs calls the getValue function of the component. If you look at getValue for Checkbox it simply returns:
What I basically want is getValue to behave like getSubmitValue():Code:getValue: function() { return this.checked; }
In that case yes, inputValue and unchecked would work.Code:getSubmitValue: function() { var unchecked = this.uncheckedValue, uncheckedVal = Ext.isDefined(unchecked) ? unchecked : null; return this.checked ? this.inputValue : uncheckedVal; }, isChecked: function(rawValue, inputValue) { return (rawValue === true || rawValue === 'true' || rawValue === '1' || rawValue === 1 || (((Ext.isString(rawValue) || Ext.isNumber(rawValue)) && inputValue) ? rawValue == inputValue : this.onRe.test(rawValue))); },
-
21 Dec 2012 11:06 AM #4
I ended up using this convert function on the model field:
Code:function (val, record) {if (val === true) return 1; if (val === false) return 0; return val;}
-
21 Dec 2012 11:13 AM #5
Just as a side note, I think a better solution would have been to provide some config properties called:
trueValue - this would be defaulted to true
falseValue - this would be defaulted to false
trueValues - array of values equivalent with "true"
The name inputValue is confusing. uncheckedValue is less confusing but still.
Then change the getValue function to return the trueValue if the checkbox is checked otherwise to return the falseValue.


Reply With Quote