I have a hidden field on my form that is required. The hidden field is populated when a user selects a value from a textbox using jQuery UI autocomplete. I have setup my form validator to not ignore hidden fields so that the validation actually works. The validation does work, but when a user selects the desired option from the autocomplete, which then populates the hidden field, the validation message will not clear and will show until the form is posted. Do I need to manually call $('form').validate() or something once the hidden field gets set or is there a way to have this clear without any extra javascript code?

1 Answer
1

The reason the error message remains is because there are none of the normal validation triggers on a hidden field. Normally, for a single field, validation is triggered on key-up or on blur. The field is hidden so only the submit button will trigger a validation test of the hidden field when the whole form is validated.

However, you can programatically trigger a validation test on any field by calling the .valid() method at any time.

Click 'submit' to show the "required" message on the hidden input called field2, which has no value.

Clicking the "go" button programatically puts a value in the hidden input. The form would now technically pass validation (upon submit) but the error message remains because we have no validation trigger.

Trigger validation by calling .valid() immediately after putting a value in the hidden field. This forces a validation test of the hidden field, which then hides the error message (as long as the validation rules are satisfied).