Hi I am using MVC4 with client side validation. This works great for showing validation messages next to the fields.
I have added a validation summary:
@Html.ValidationSummary(false)
This works, client side 'n all. I would like it to behave differently though; currently the messages in the validation summary only change when the submit button is clicked. I would like them to be dynamically, populated in a similar fashion to each field's validation message.
Can someone suggest a way to achieve this ?
Any information on what triggers the summary update would be great.
The ValidationSummary class is used to summarize the error messages from all validators on a Web page in a single location. You can summarize the error messages from a group of validators on a Web page by assigning the ValidationSummary control to a validation group by setting the ValidationGroup property.
ValidationSummary control Syntax :Step 1 – Open Visual Studio –> Create a new empty Web application. Step 2 – Create a new web form and design a web form as shown in below screen. Step 3 – Drag and drop ValidationSummary control from Toolbox. List of Properties of ValidationSummary control.
Implementation Details. Main Features. ValidationSummary is an extension that allows you to summarize validation errors from multiple data editors.
ASP.NET MVC: ValidationSummary The ValidationSummary() extension method displays a summary of all validation errors on a web page as an unordered list element. It can also be used to display custom error messages.
The client side summary seems to be generated when the whole form is validated, and you can do this yourself by calling the valid() plugin method. Add this after the jquery validation scripts.
<script>
$(function () {
$(':input').on("keyup click", function () {
$('form').valid();
});
});
</script>
The example events I have included are keyup and click but you can add to or remove from this space separated list.
I've set up the validation summary to update in 'real time' also considering the following:
Let's extract the validator, overrride showErrors() and implement our logic:
var validator = $('form').data('validator');
validator.settings.showErrors = function (map, errors) {
this.defaultShowErrors();
if ($('div[data-valmsg-summary=true] li:visible').length) {
this.checkForm();
if (this.errorList.length)
$(this.currentForm).triggerHandler("invalid-form", [this]);
else
$(this.currentForm).resetSummary();
}
}
Since I'm using this solution for my entire site I've created the following init (onready):
$('form').each(function () {
var validator = $(this).data('validator');
if (validator) {
validator.settings.showErrors = function (map, errors) {
this.defaultShowErrors();
if ($('div[data-valmsg-summary=true] li:visible').length) {
this.checkForm();
if (this.errorList.length)
$(this.currentForm).triggerHandler("invalid-form", [this]);
else
$(this.currentForm).resetSummary();
}
};
}
});
And here's the resetSummary used above:
jQuery.fn.resetSummary = function () {
var form = this.is('form') ? this : this.closest('form');
form.find("[data-valmsg-summary=true]")
.removeClass("validation-summary-errors")
.addClass("validation-summary-valid")
.find("ul")
.empty();
return this;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With