PDA

View Full Version : Custom validation problem



Achim74
24 Feb 2011, 3:00 AM
Hello !

I set a custom vtype for validation. The goal is to force the user to enter a value either in one field or into another but not into both.



Ext.apply(Ext.form.VTypes, {
redirect: function(value, field)
{

if (field.name=='link') {
var check = TestForm.getForm().findField('redirect');
}
if (field.name=='redirect') {
var check = TestForm.getForm().findField('link');
}

var check_value = check.getValue();

if (value=='' && check_value=='') {
this.redirectText = 'Enter a value. Both fields are empty.';
return (false);
}

if (field.value!='' && check_value!='') {
this.redirectText = 'Please enter a value either in one field or the other';
return (false);
}

return (true);
},

redirectText: 'Please enter a value either in one field or the other'
});


This works so far. Both textfields are defined with allowBlank:true.

I have a problem when I submit the form and leave both fields blank. The fields do not get validated. When I set allowBlank to true this overrides my custom validation. Manually calling .validate() or validateValue() gives me always true.

What am I missing for an elegant solution? Thank you for your hints !

Greetz
Achim

Condor
24 Feb 2011, 3:40 AM
Unfortunately the allowBlank tests is executed before the vtype test, so the only way you could do this is by initializing both fields with allowBlank:false and change it to true in the vtype test.

ps. Another way would be to use the 'validator' function instead of a 'vtype' (the validator is executed before the allowBlank test).

Achim74
24 Feb 2011, 4:49 AM
validator: function(value) {
var check_value = TestForm.getForm().findField('link').getValue();

if (value=='' && check_value=='') {
this.setActiveError('Both fields are empty',true);
return (false);
}

if (value!='' && check_value!='') {
this.setActiveError('Please fill either one or the other',true);
return (false);
}

return (true);

}


Thank you! The validator did it !

Now I have the problem that I cannot set the custom error message in the Quicktip. I tried setActiveError and markInvalid. Both were not successful. Do you have another hint?

Condor
24 Feb 2011, 5:05 AM
The validator should return the error message instead of false.

Achim74
24 Feb 2011, 5:19 AM
Oh well .. reading the API carefully sometimes could help :)
I've totally overlooked this .. thank you very much!