PDA

View Full Version : Validation on ComboBox



StuartMilne
10 Feb 2010, 2:41 AM
Hi Guys

I have a comboBox on my form with a validator function attached. My problem is that every time the form is loaded the validator function is called.

How do I change it so that the validator function is only called when my Save button is clicked.

Kind regards
Stuart

StuartMilne
10 Feb 2010, 3:49 AM
Can anyone help with this? Its probably something obvious but i cant seem to figure it out.

Eugen_
10 Feb 2010, 4:17 AM
Hi StuartMilne,

Post some code here to help us find errors

StuartMilne
10 Feb 2010, 4:40 AM
Here is my combo box:


var conditionValueCombo = new Ext.form.ComboBox({
id: 'conditionvalue',
name: 'conditionvalue',
mode: 'local',
width: 350,
editable: false,
hideLabel: true,
forceSelection: true,
selectOnFocus: true,
triggerAction: 'all',
store: valueStore,
valueField: 'condValue',
displayField: 'name',
useID: true,
value: '{/literal}{$conditionValueDefault}{literal}',
mode: 'local',
validator: comboValidate,
post: true
});

And here is my function:


function comboValidate(string)
{
if (string == '{/literal}{#str_LabelMakeSelection#}{literal}')
{
return false;
}
else
{
return true;
}

}

StuartMilne
10 Feb 2010, 5:13 AM
Alternatively is there any other way to validate a comboBox so that if the default value is selected then validation fails.

StuartMilne
10 Feb 2010, 6:15 AM
Anyone?

Eugen_
10 Feb 2010, 8:10 AM
How do I change it so that the validator function is only called when my Save button is clicked.

remove validator from conditionValueCombo and check value in send button handler before submitting as follows



if (comboValidate(conditionValueCombo.getValue())) {
submitting...
} else {
conditionValueCombo.markInvalid();
}

StuartMilne
10 Feb 2010, 8:22 AM
Thanks for the response. However what I didnt say which I should have is that I have 4 comboBoxes on the form calling the same validator function.

So is there any way to do this without having to do an IF condition for each of the comboBoxes?

Eugen_
10 Feb 2010, 8:46 AM
function comboValidate(combo)
{
if (string == '{/literal}{#str_LabelMakeSelection#}{literal}')
{
combo.markInvalid(); return false;
}

return true;
}



if (comboValidate(conditionValueCombo1) && comboValidate(conditionValueCombo2) &&
comboValidate(conditionValueCombo3) && comboValidate(conditionValueCombo4)) {
submit...
}

or use each (http://www.extjs.com/deploy/dev/docs/source/MixedCollection.html#method-Ext.util.MixedCollection-each) function
but the best way to fix this error - set valid values by form loading