Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Validator- Rule specific errorPlacement

I'm using jquery validator.

I've got a custom rule applied to a group of elements in my form. These inputs may also have other (standard) rules associated with them. I need to place the error message in one place if the element fails my custom rule, but another place if the element fails a standard rule.

I need something like -

errorPlacement: function(error, element) {
    if (error == MyCustomError) {
        // put my error at the top of the form
    } else {
        // put my error next to the element
    }

I can't see what that 'error' object really is (tried drilling down with firebug but it wasn't very informative).

The highlight option is even more difficult because I don't even have the 'error' parameter to use.

like image 629
Aidan Ewen Avatar asked Apr 26 '12 14:04

Aidan Ewen


1 Answers

error is the whole error label HTML element that is being added by jQuery Validate, i.e.:

<label class="error">Error message here</label>

So if you want to place the message differently if the message is "Do not do this", you can:

errorPlacement: function(error, element) {
if (error.text() == "Do not do this") {
    error.insertBefore("SELECT FIRST ELEMENT IN THE FORM HERE USING STANDARD JQUERY SELECTORS");
} else {
    error.insertAfter(element);
}
like image 74
Smilyan Avatar answered Nov 16 '22 18:11

Smilyan