Add error message to @Html.ValidationSummary
Asked Answered
C

2

28

I'm using standard MVC3 Razor views with unobtrusive Javascript validation, using @Html.ValidationSummary to show them at the top of the form. If the standard validations (things like [Required]) pass, I then run some very custom client-side validation that fires when the user hits the Submit button. (The validation looks across a number of form elements to make sure that the proper set of them have been checked, etc., so it's not as simple as just creating a new custom validator for a single field).

I'd like the possible error(s) I construct there to be shown in the ValidationSummary list, but I can't figure out how to get the error message to appear there.

Cheju answered 24/10, 2011 at 21:10 Comment(1)
In client-side or server-side?Leopold
L
45

In client-side:

function YourCustomValidator() {
    // do your validation logic here via JavaScript
    return true; // or false based on your validation logic
}
$(document).ready(function () {
    // take your own form-selector like ("form", this)
    $("form", this).first().submit(function () {
        return (YourCustomValidator() && $(this).valid());
    });
});

OR In server-side:

Think you have a model like this:

public class Test {
    [Required]
    [StringLength(100)]
    public string FullName { get; set; }
}

and when you are validating it:

if(ModelState.IsValid) { // default validations run here
    if(/* some custom validations run here, there is an error about "FullName" */){
        // you should set the "key" for Model-Error to "FullName"
        ModelState.AddModelError("FullName","error-message goes here")
    }
    if(/* some custom validations run here, the error is global, not on "FullName" */){
        // you should set the "key" for Model-Error to an empty-string
        ModelState.AddModelError("","error-message goes here")
    }
    // also you can test for model-errors again like this:
    if(ModelState.IsValid) { // if you add any error above, this will be "false"

    }
}
Leopold answered 24/10, 2011 at 21:29 Comment(3)
Thanks - I'm looking for just client-side in this case. My question is if I DO find an error in "YourCustomValidator", how do I get my specific error message to appear? I already have server-side validation, which allows me to set any error message I want via "AddModelError", but I want to do the equivalent in Javascript.Cheju
You can create a custom logic to show an error-message via JS and/or you can create custom validation-attribute class via C#. which one do you mean?Leopold
I don't know why this has 32 votes, it does not answer the question at all.Valadez
E
6

You can do so just adding the Error Message to the ModelState should display the error message for you, provided that you have ValidationSummary() called on your view.

To add the error to the ModelState just do this:

ModelState.AddModelError("ColumnNameOrErrorMessageKeyInState","error message goes here")
Endgame answered 3/4, 2014 at 1:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.