PDA

View Full Version : [CLOSED] Checkbox values always set to false in record when using form.updateRecord



kleinsch
22 Aug 2011, 3:50 PM
Sencha Touch version tested:

1.1.0


only default ext-all.css


Platform tested against:

Chrome
iOS4


Description:

Checkbox values always get set to false in the record object when calling form.updateRecord. Looked into the code, and getValues returns "" for a checked checkbox field, which gets converted to false when setting a boolean field.


Test Case:




Ext.ns('MyApp');


MyApp.User = Ext.regModel('User', {
fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'},
{name: 'active', type: 'boolean'}
],


proxy: {
type: 'memory'
}
});


Ext.regStore('User', {
model: "User",
data: [
{
id: 1,
name: "nick",
active: true
},
{
id: 2,
name: "jim",
active: false
}
]

});


myFormType = Ext.extend(Ext.form.FormPanel, {


reload: function(record) {
this.record = record;
this.load(record);
},


onUpdateForm: function() {
var record = this.getRecord();
this.updateRecord(record);

console.log('Active: ' + record.data.active);
record.data.performed = true;
record.save();
},

initComponent: function() {


Ext.apply(this, {
items: [{
xtype: 'textfield',
name : 'name',
label: 'Name'
}, {
xtype: 'checkboxfield',
name : 'active',
label: 'Active'
},
{
xtype: 'button',
text: 'Update',
ui: 'confirm',
handler: this.onUpdateForm,
scope: this
}
]
});


myFormType.superclass.initComponent.apply(this, arguments);


this.reload(Ext.getStore('User').getById(1));
}
});
Ext.reg('user-form', myFormType);


new Ext.Application({
launch: function() {
new Ext.Panel({
fullscreen: true,
items: [
{
id: 'myuserform',
xtype: 'user-form',
}
]
});
}
});



Steps to reproduce the problem:

Run provided test case, look in console output after touching update button.


The result that was expected:

Console output should match checkbox state on the screen.


The result that occurs instead:

Console output always says false, even if checkbox is selected


Possible Fix:

Modify FormPanel:getValues() to set true or false for checkbox fields, instead of "" or NULL


Ext.form.FormPanel.override({
getValues: function(enabled) {
var fields = this.getFields(),
field,
values = {},
name;


for (name in fields) {
if (fields.hasOwnProperty(name)) {
if (Ext.isArray(fields[name])) {
values[name] = [];


fields[name].forEach(function(field) {
if (field.isChecked() && !(enabled && field.disabled)) {
if (field instanceof Ext.form.Radio) {
values[name] = field.getValue();
} else {
values[name].push(field.getValue());
}
}
});
} else {
field = fields[name];

if (!(enabled && field.disabled)) {
if (field instanceof Ext.form.Checkbox) {
values[name] = field.isChecked();
} else {
values[name] = field.getValue();
}
}
}
}
}


return values;
}
});

Orbiiter
25 Aug 2011, 5:15 PM
According to the documentation, you're supposed to use a 'value' field when the checkbox is checked.


...

{
xtype: 'checkboxfield',
name : 'active',
label: 'Active',
value: true
}

...

This appears to work.

mike.estes
26 Aug 2011, 3:26 PM
@kleinsch, @Orbiiter is correct, you need to specify what the value would be when checked. I've marked this thread as closed

kleinsch
29 Aug 2011, 7:16 AM
Yup, that works. Don't know how I missed that.

I guess this isn't a bug, but in general a checkbox is what's going to be used for a boolean field in a model, so I think it'd make sense for it to work out of the box without having to specify a value. Maybe it'd make more sense for the default to be "true" or "checked" instead of "" so boolean fields work by default?

epiphanydigital
14 Sep 2011, 9:04 AM
It took me doing the same thing to stumble upon the "checked: true" option. I was thinking value would determine if it was true or false, not what checked meant. Not intuitive, but not a big deal.

johnseelewis
15 Sep 2011, 6:57 AM
Why not have something by default? This seems a strange requirement.

epiphanydigital
15 Sep 2011, 7:00 AM
There is a default...



checked: true // This is the default value
value : false // When the checkbox is checked, it returns the value "false"