I'm experimenting with using ext_scaffold to generate UI elements for a Rails-backed web application.
However, I'm experiencing issues with the behavior of boolean fields. For instance, when I do
./script/generate ext_scaffold product name:string description:text sku:integer available:boolean
It builds the migration properly and generates a user interface with a checkbox for the boolean field.
Checking this box, however, only modifies the object if it's being created. If the record already exists, the state of the checkbox accurately reflects the field. However, editing it is failing -- i.e., checking or unchecking it and saving the record doesn't change it.
Dipping into the code, we find:
items: [
{ fieldLabel: scaffoldPanel.labels['product[name]'], name: 'product[name]', xtype: 'textfield' },
// ... other fields ...
{ fieldLabel: scaffoldPanel.labels['product[available]'], name: 'product[available]', xtype: 'checkbox', inputValue: '1' }, { xtype: 'hidden', name: 'product[available]', value: '0' }
],
I sense the problem is that either Rails or Ext is confused at the identically-named elements. Either way, after a record has been created, clicking the check box does nothing (checked or unchecked, the field remains '0'.)
After playing around a bit with this, it turns out if we place that hidden field ABOVE the checkbox, no matter what the field now gets set to true after editing. Not exactly desired behavior either.
Has anyone else experienced this issue? Are there any workarounds? Thanks.
UPDATE:
Note that even with the checkbox for the field checked, the POST request sent indicates the field is set to false. All other field types are getting updated appropriately...
UPDATE2:
Alright, found a great blog post describing Semergence's solution to this problem, but I'm still not quite able to get this to work... I've adapted his solution as follows:
onOk: function() {
// ....
if (scaffoldPanel.getFormPanel().currentMode == 'edit') {
// set up request for Rails create action
submitOptions.params._method = 'PUT';
submitOptions.url = submitOptions.url + '/' + selected.data.id;
}
// ----- checkbox serializer ------
var serializedForm = scaffoldPanel.getFormPanel().getForm();
// because unchecked checkboxes do not submit values, manually force a value of 0
serializedForm.items.each(function(f) {
//alert("Checking \"" + f.getName() "\" field...");
if (f.isFormField && f.getXType() == 'checkbox' && !f.getValue()) {
alert("Auto setting \"" + f.getName() + "\" field to 0...");
serializedForm.getValues()[f.getName()] = '0';
}
});
// ------- end serializer -------
serializedForm.submit(submitOptions);
//scaffoldPanel.getFormPanel().getForm().submit(submitOptions);
},
Now the alert gets thrown properly, indicating the correct field and asserting it will be set to zero.
Except this doesn't occur down in the post request which actually gets sent -- in fact, if the checkbox is unchecked, it doesn't have a value in the POST at all.
Can someone help me understand what's going on here?