Reputation: 15452
Using the showErrors() function of JQuery validation, I am manually adding errors to my form:
var errors = {};
errors['Field1'] = 'Field1 has an error';
errors['Field2'] = 'Field2 has an error';
errors['Field3'] = 'Field3 has an error';
$('form').validate().showErrors(errors);
Why can't I then remove these using $('form').validate().resetForm();
? The only way of doing this seems to be to reset all the indexes to null:
var errors = {};
errors['Field1'] = null;
errors['Field2'] = null;
errors['Field3'] = null;
$('form').validate().showErrors(errors);
Upvotes: 2
Views: 1900
Reputation: 98748
When using the showErrors()
callback function, anything you manually do with this callback function, you must also manually "undo".
The .resetForm()
method only resets the validation plugin by clearing out the invalid status of the fields. Since the default error messages are automatically handled by the plugin, these will simply vanish as a result of the resetForm()
call.
Since you're not using the automatically generated errors, the resetForm()
will have no effect on your custom message summary.
However, there is another option called errorContainer
that will toggle along with the errors. If you put your error summary inside this container, it will automatically toggle itself along with the errors.
HTML:
<div id="summary"></div>
jQuery:
$(document).ready(function () {
$('#myform').validate({
// other rules and options,
errorContainer: "#summary", // will toggle along with errors
showErrors: function (errorMap, errorList) {
// your custom function
}
});
});
DEMO: http://jsfiddle.net/JQjWu/
Upvotes: 2