jwaliszko / ExpressiveAnnotations

Annotation-based conditional validation library.
MIT License
351 stars 123 forks source link

Possible to Check Validation State? #117

Closed Rumple-Phillip closed 8 years ago

Rumple-Phillip commented 8 years ago

May be a stupid question, but is it possible to check the validation state after the validation has been ran? Ergo, we want our submit button disabled (which we can do with jquery) when clicked, and not be clickable again unless the validation fails. This helps prevent multiple submissions into our system. But, for the life of me, I can't figure out how to query the state of the client side validation :/

jwaliszko commented 8 years ago

If I understand you correctly, are you looking for jquery.valid() method? Calling this function checks whether the selected form is valid, or whether all selected elements are valid, i.e.

var isValid = $('form').valid();

This call above will re-validate your form though. If the form has already been processed by validation function, and you do not wish the validation to trigger again, you can call the below statement instead, i.e

var validator = $('form').validate(); // gets validator object attached to the form
var isValid = validator.numberOfInvalids() === 0;

You can also count the displayed error messages, etc.