Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am using the jquery validation plugin and want to use the errorPlacement function to add error messages to the fields title attribute and display just a ? next to the field.

This works great when the form is submitted with the submit button but when any of the following events are triggered: - onfocusout - click - onkeyup

The validation checks are run but it skips the errorPlacement function and adds the full error message after the field, like the default behaviour.

I am using the following code:

$("#send-mail").validate({
    debug: true,
    // set this class to error-labels to indicate valid fields 
    success: function(label) {
        // set text as tick
        label.html("✔").addClass("valid"); 
    }, 
     // the errorPlacement has to take the table layout into account 
    errorPlacement: function(error, element) {
        console.log("errorPlacement called for "+element.attr("name")+" field");
        // check for blank/success error
        if(error.text() == "")
        { 
            // remove field title/error message from element
            element.attr("title", "");
            console.log("error check passed");
        }
        else
        {
            // get error message
            var message = error.text();

            // set as element title
            element.attr("title", message);

            // clear error html and add cross glyph
            error.html("✘"); 

            console.log("error check failed: "+message);
        }
        // add error label after form element
        error.insertAfter(element);
    },
    ignoreTitle: true,
    errorClass: "invalid"
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
253 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your problem is that the plugin only calls the errorPlacement function once for each element which is validated. Namly when the error label for the element is first created. Afterwards the plugin just reuses the already present label and just replaces the html inside (or hides the error label if the element is now valid). That's why your cross gets removed and the actual error message is shown.

Just to make sure the flow of the plugin is clear.

  1. element (no errorlabel yet)
  2. element gets validated at some point
  3. plugin creates error label and calls errorPlacement function
  4. element "cross" (error message in title)
  5. Element gets focus and you change something
  6. plugin revalidates element
  7. Sees that error label was already created (and placed)
  8. plugin just calls label.html(message) instead of removing old label and readding it

So you see your problem is a kind of optimization the plugin does to save some unnecessary inserts/removes of error labels. Which makes sense too.

You can check what I said by looking at the validation-plugin-sourcecode

jquery.validate.js v1.6 check in function showLabel lines 617-625 for the relevant pieces.


A possible solution could be to additional provide a custom showErrors callback which solves the problem with brute force.

Something along the lines of

$("#send-mail").validate({
...
    showErrors: function(errorMap, errorList) {
        for (var i = 0; errorList[i]; i++) {
            var element = this.errorList[i].element;
            //solves the problem with brute force
            //remove existing error label and thus force plugin to recreate it
            //recreation == call to errorplacement function
            this.errorsFor(element).remove();
        }
        this.defaultShowErrors();
    }
...
});

Maybe there is a cleaner solution to this but this should do it and give you time to investigate a better solution.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...