2

I would like to add the name of the errors to my function so users know which fields they have to check.

This is my current function:

showErrors: function(errorMap, errorList) {
            var summary = "Please check following errors:";
            $.each(errorList, function() {
                summary += " * " + this.message + "<br>" ;
            });
            $('.errorbox').html(summary);
            this.defaultShowErrors();
        },
        submitHandler: function() {
            alert("submitted!");
        }
    });

How can I do this?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
logistef
  • 746
  • 1
  • 7
  • 20
  • I had no idea that i had to mark as solved. Thanks for letting me know! The fiddle that mansuUK made in a previous question shows how its implemented now. http://jsfiddle.net/manseuk/xTdYr/2/ – logistef Nov 07 '11 at 10:49

2 Answers2

2

As you are using the Bassistance.de validation plugin, the easiest option to you is to add a friendly insturction message to the title parameter to each input control. This message will then be used as instruction text in the error message which appears. Here is an updated fiddle showing how this works: http://jsfiddle.net/xTdYr/3/

Alternatively, you can manually add the rules in the instanciation call of the .validate() method by using the messages parameter, details here: http://docs.jquery.com/Plugins/Validation/rules

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • Recommended. If that isn't possible you could take the name attribute like `summary += " * " + this.element.name + ": " + this.message + "\n";` – Marco Johannesen Nov 07 '11 at 11:09
  • The form is being processed in different languages so using the messages is not the best option as I load the standard messages from the different language files. This way the error feedback looks quite the same. Is there really no option adding the name value to the feedback message? – logistef Nov 07 '11 at 11:51
  • Marco's answer worked for me. Thanks for the other suggestions people. – logistef Nov 07 '11 at 14:42
0

Extend options with messages: {name:"name error"}

Live Demo: http://jsfiddle.net/gsBTC/

$('#newform').validate({
    errorPlacement: function(error, element) {
        if (element.is(":radio")) {
            error.prependTo(element.parent());
        }
        else { // This is the default behavior of the script  
            error.insertAfter(element);
            console.log('here');
        }
    },
    showErrors: function(errorMap, errorList) {
        var summary = "You have the following errors: \n";
        $.each(errorList, function() {
            summary += " * " + this.message + "\n";
        });
        $('#errordiv').html(summary);
        this.defaultShowErrors();
    },
    submitHandler: function() {
        alert("submitted!");
    }, 
    messages: {
     name   : "name error", 
     subject: "subject error",
     message: "message error"
   }
});
enloz
  • 5,704
  • 9
  • 37
  • 56
  • The problem is that the form is multi-language. So I'm using the language files for the different languages. I could add a specific message for every element but i have 70 elements in 3 differernt langueges. That's alot of messages. So i prefer to use the general messages in each language and add the "name" so the user knows wich field is not ok. – logistef Nov 07 '11 at 13:09