I like jQuery validation for its simplicity and extensibility. In some of my last use cases it seemed to reach its limits though. There are some features that I am missing. My question: Is there an open-source library that does support these features, or is there anything remarkable in development? Or, do you have any smart hints on how to extend/use the validation framework to cover these features?
Missed features:
-
For some fields I need to define “warning” rules rather than erros. If they evaluate to false, a message should be displayed but it should contain a link “ignore” that allows the user to turn off the rule (similar to MS Word’s spellchecker where the user can choose to ignore a spelling error). If the validation prevented the form from being sent, ideally the form is immediately sent when the user clicks “ignore”.
-
The rules should depend on the action I want to execute with the form. Whilst for one action the fields are not required yet they might be required to execute another action.
As an example for both cases, take a “send e-mail” form:
- The “subject” field is not necessarily required but if it is empty the user might have missed it. Ideally the validation framework blocks the form from being sent and displays a message such as “You missed the subject. Ignore”. By clicking “Ignore” the message is disabled and the form is sent.
- The “to” field is definitely required for sending the e-mail. But if the form provides a “save draft” button, the user should not be forced to fill in this field for saving the draft.
For number two it’s surprisingly easy. Rather than just submitting, you have each button call a function that submits on completion. In that function, you set a jquery variable to tel you which button it came from, and you use the contents of that variable in the part of the
jQuery.validator.addClassRules()call that determines whether or not to apply the validation requirement. The first is trickier, but you can set the validation text – which means that you should be able to include a link in it. Have the link trigger a function that, again, changes a variable that the apply/not-apply logic uses (and possibly resubmits – or, alternately, if you have a page with more than one of these, revalidates (thus removing the error message) and allows them to submit later with a different button).Quick example on what the addClassRules might look like.