PDA

View Full Version : discussion: how should forceSelection of a ComboBox work?



tobiu
26 Jun 2009, 7:56 AM
hi together,

since this is neither a question nore a bug report, i post this here under "general".

in ext2.x this feature was buggy (see my report under http://extjs.com/forum/showthread.php?t=64695 ), now it is working technically correct, but i am not sure if this is the best way.

in ext3-rc2 onBeforeBlur checks, if a value was selected (by clicking into the menu) and if not resets the field. if you type exactly the same text like a displayField value of the store, the field will get reset, since you did not click on the item.

i would prefer a way, where you can type text and if that text matches exactly a value of the store, "forceSelection" is fullfilled.

my ext2.x hotfix is an working approach to simulate it:



Ext.override(Ext.form.ComboBox, {
validate : function(){
var prevVal = Ext.form.ComboBox.superclass.validate.call(this);
if(prevVal === false)return false;
if(this.forceSelection && this.rendered && !this.disabled){
if(this.el.dom.value.length > 0){
if(this.store.find(this.displayField, this.el.dom.value) !== -1)return true;
if(this.store.getCount() == 0 && this.value && this.hiddenValue)return true;
this.markInvalid('not a selected value!');
return false;
}
}
return true;
}
});


the user gets the chance to type text, if it does not match a value of the store, the field will get marked invalid instead of deleting the input. to make this working in ext3, onBeforeBlur has to be modified (the forceSelection part has to be removed).

i have no problem to use overrides for my own app, but would like to hear other opinions on this.

kind regards, tobiu

mystix
26 Jun 2009, 9:32 PM
forceSelection should force the user to select a value from the Combo's list / type a value found in the Combo's list, and clear the Combo otherwise.

try this override:
(untested -- just toying with some ideas)
tested on 3.0rc2 with the examples/form/combos.html example.


Ext.override(Ext.form.ComboBox, {
// handle setting of the Combo's value if, after typing some value, the field is blurred via mouseclick
// and no selection was made from the Combo's list by either
// a) clicking in the list
// or
// b) tabbing on a highlighted list value
beforeBlur : function() {
var val = this.getRawValue();

if (this.forceSelection && !this.findRecord(this.displayField, val)) {
// typed-in value does not exist in Store, so clear the Combo
this.clearValue();
} else {
// note: since setValue() already handles Record lookup,
// setting of emptyText / valueNotFoundText etc etc etc we might as well just use it
this.setValue(val);
}
}
});