PDA

View Full Version : Marking a textfield invalid



Nullity
7 Jul 2011, 5:03 AM
I searched for this before posting, and while I found a few similar questions, none of them had any/satisfactory answers. So if I missed something, I apologize.


I have two textfields, we'll just call them A and B. Field B is required only on certain values of A. I had a couple ideas on how to accomplish this, but both are presenting me with issues.

The first idea was to set an onBlur listener on field A that calls "markInvalid()" on field B when necessary. However, this presents a problem since "markInvalid()" is visual only, it doesn't actually "mark" the field invalid to stop the form from being submitted (it seems to me that it should).

The second idea was to write a custom validator for field B which checks the value of field A to handle it. However, this presented a different problem. A custom validator overwrites any automatic validators that would have run. For instance, field B also has an input regular expression mask, and the maxlength option set. Setting a custom validator effectively clears these.

So, how can I either: 1 - truly mark a field invalid so that the form's validation check fails on submit, or 2 - write a custom validator function that still calls all previously set validations?

Or, is there a 3rd option I haven't considered?

pavanextjs
7 Jul 2011, 10:37 AM
A.on('change', function(field, newValue, oldValue){
B.allowBlank = false;
B.validate();
});

'change' event is fired just before the 'blur' event only if the value is actually changed, meaning 'change' won't fire for tab-in & tab-outs.

Nullity
7 Jul 2011, 2:08 PM
Awesome, that worked. Thank you very much.