PDA

View Full Version : [3.1.1] setValues sets any value into a combobox



buergi
18 Feb 2010, 2:25 AM
Hi

I have a combobox with forceselection: true. If I use form.setValues and the value for the combobox is not from the selection, the value will be set anyway.

The combobox is still valid and with combo.getValue() I receive the value I set before.

How can I ensure with setValues(), that a combobox allows only values from the selection?

If I put the same value with combo.setValue() it doesn't work...

evant
18 Feb 2010, 2:39 AM
Please post a test case that demonstrates the issue, see: http://www.extjs.com/forum/showthread.php?t=71015

buergi
18 Feb 2010, 10:53 AM
new Ext.FormPanel({
renderTo: Ext.getBody(),
id: 'foo',
items: [{
xtype: 'combo',
name: 'bar',
id: 'bar2',
dataFields: ['key', 'value'],
store: [['1', '11']],
fieldLabel: 'label',
displayField:'value',
valueField: 'key',
forceSelection: true,
allowBlank: false,
typeAhead: true,
mode: 'local',
triggerAction: 'all',
selectOnFocus: true
}]
});

Ext.getCmp('foo').getForm().setValues({bar: 'wrongValue'});

console.log(Ext.getCmp('bar2').isValid());
console.log(Ext.getCmp('bar2').getValue());


the field is valid and getValue is 'wrongValue'

buergi
22 Feb 2010, 12:41 AM
?

danellison
22 Feb 2010, 10:36 AM
It seems to me that what is called for here is an override of the combobox setValues method or to create a custom vtype that insures that the current value is contained in the combobox store.



Ext.override(Ext.form.ComboBox, {
setValues: function(val) {
var validvalues = this.getStore().getRange(0, this.getStore.getCount()-1);
var validatedval = false;
Ext.each(validvalues, function(testval){
// this logic must match with your object's properties
if(testval == val) {
validatedval = true;
return false;
}
});
if(validatedval) this.superclass.setValues.call(this, val);
else return false;
}
});


Off the top of my head - may not be 100% accurate but should get the idea across. Hope it is helpful.

Dan

buergi
23 Feb 2010, 12:11 AM
Thanks, I'll try that out.

But shouldn't be this the default behaviour?

tobiu
23 Feb 2010, 3:32 AM
hi buergi,

it is even a bit more complicated ;)

comboBoxes allow setValue, even if the store is not loaded.
that's the reason, each value is shown in the textfield, assuming that you only set correct ones. it also makes trouble with distinguishing between hidden / valueFields.

i hope, a general fix will come soon.
the suggestion of animal and saki is a good start:

http://www.extjs.com/forum/showthread.php?t=59410


kind regards,
tobiu