PDA

View Full Version : [FIXED] FormPanel's setRecord doesn't set numberfield or selectfield



yoh.suzuki
21 Oct 2011, 5:49 PM
When I load my Ext.data.Model instance into the Ext.form.Panel, the number fields and select fields don't get set correctly.

Here's some code that demonstrates this behavior:



Ext.setup({
onReady: function() {
Ext.define('SelectModel', { extend: 'Ext.data.Model', fields: [
{ name:'V', type:'int' },
{ name:'D', type:'string' }
]});
Ext.define('DemoModel', { extend: 'Ext.data.Model', fields: [
{ name:'String', type:'string', defaultValue:'Fantastic' },
{ name:'Number', type:'int', defaultValue:10000 },
{ name:'Select', type:'int', defaultValue:0 }
]});
var fieldset, form = Ext.create('Ext.form.Panel', {
fullscreen: true,
items: [
fieldset = Ext.create('Ext.form.FieldSet', {
title: 'wut',
items :[
{
xtype: 'textfield', name: 'String',
label: 'String (correct)'
},
{
xtype: 'numberfield', name: 'Number',
label: 'Number (incorrect)'
},
{
xtype: 'selectfield', name: 'Select', valueField:'V', displayField:'D',
label: 'Select (incorrect)',
store: Ext.create('Ext.data.JsonStore', {
model: 'SelectModel',
data: [
{V: 0, D: 'Selection_0'},
{V: 1, D: 'Selection_1'},
{V: 2, D: 'Selection_2'}
]
})
}
]
}),
{
xtype:'button',
text:'try loading a record',
handler: function() {
var record = Ext.create('DemoModel', {});
form.setRecord(record);
fieldset.setTitle(Ext.encode(record.data));
this.disable();
}
}
]
});
}
});

yoh.suzuki
21 Oct 2011, 6:28 PM
It looks like Ext.form.Number is not taking the value from the record because of minValue and maxValue. The workaround is to add the following to its configuration:


minValue:-9007199254740992,
maxValue: 9007199254740992

yoh.suzuki
21 Oct 2011, 7:02 PM
I didn't have a chance to hunt down a real solution, but here's a workaround for Ext.form.Select. You can call the selectfield's onPickerChange function with its picker and the record's data to get it to work. I wrote a function like this:


// the hackiest of hacks
function setSelectFieldValue(selectfield, record) {
var s = selectfield, r = record, store = s.getStore(),
v = s.getValueField(), n = s.getName();
!!(store.find(v, r.data[n])+1) && s.onPickerChange(s.picker, r.data);
}

And I'm calling it when I want to setRecord.


form.setRecord(record); // <-- fills out most of the form correctly
setSelectFieldValue(selectfield, record); // <-- sets the selectfield correctly

(Disclaimer: this is not a good solution. It's just a hacky workaround. I wanted to share in case others are running into this.)

Jamie Avins
31 Oct 2011, 10:57 AM
Thank you for the report.