Validation Message is shown even there is no error message - asp.net-mvc

I have created a form along with other fileds having validation and there is one field where it doesn't have the validationMessageFor but in validationSummary I am able to see the error for that field and model doesn't have the required property.

Validation summary Is supposed to display all the error messages on the model irrespective of validationMessageFor usage in UI.
The field might contain any other validations associated with it. Please post your model here.

Related

MVC Show different ValidationSummary ErrorMessage compared to ValidationMessage

In ASP.NET MVC, is it possible to show a different message in the ValidationSummary compared to what is shown in the ValidationMessage?
IE - if i have a FirstName textbox, on validation the message next to the text box will say 'You need to fill this out', but in the validation summary it will say 'Please provide a first name'.
I'm not sure if I completely understand what you are trying to accomplish, but you can specify a generic error message in the validation summary. In your view you can use:
#Html.ValidationSummary(true, "Please correct the errors below")
The boolean parameter indicates whether you want to exclude property errors. The string is the message you want displayed. Using this overload the way I have above, the model-level error message Please correct the errors below would be shown in place of the #Html.ValidationSummary() method, and the property errors would be shown where you place your #Html.ValidationMessageFor() methods.
See the MSDN documentation for a complete list of overloads.
Yes. It should be quite obvious from looking at the intellisense.
you would say:
#Html.ValidationSummary(true)
and it will contain the error messages that are located on the model or the default messages
And you can say:
#Html.ValidationMessageFor(m => m.Property, "This is a custom message")
And that overrides the message on the individual message.
Also, keep in mind that #Html.ValidationSummary(true, 'Header Message') will show your custom summary message along with the same messages that you provided in your model data annotations.
i.e. in your model class:
[Required(Message="First name is required")]
public string FirstName { get; set; }
Your validation summary would look some like this:
Header Message
First name is required
Alternatively, you can build up a custom collection of messages by using ModelState.AddModelError("Key", "Message") in your controller, then referencing that key in your view using ViewData.
After looking at the Metadata available, I don't think what I want to do is possible. What I'm going to do instead is type in the input specific value for validation, and hide the class field-validation-valid.
Providing different Errormesages beside the input fields and in the Validation Summary is not possible with #Html.ValidationSummary. Such a feature could make sense because a part of the errormessage's information can come from it's position in the page (e.g. beside a firstname input field the message could be 'Input required' and in the validation summary you need 'Input required for firstname'). Unfortunately a poor implementation for ValidationSummary in MVC, .net 2.0 provided the feature to have different messages in it's validators and in the validationsummary .

ASP.NET MVC jQuery validation: how to show one message for all required fields

I got many [Required] fields in my model.
And, in traditional MVC way, I will add #Html.ValidationMessageFor() for each required textbox.
Then, if user doesn't type anything for 5 textboxes, 5 message will show up in the UI.
My question is: can I show just one message saying Please fill mandatory fields in the UI instead of showing the message everywhere like Name is required, Address is required...?
and I need this to be validate on Client Side, without post form back to server
You might want to look at ValidationSummary - http://msdn.microsoft.com/en-us/library/system.web.mvc.html.validationextensions.validationsummary(v=vs.108).aspx - Returns an unordered list (ul element) of validation messages that are in the ModelStateDictionary object. You can override the message like so:
#Html.ValidationSummary(true, "Invalid form")
Source https://stackoverflow.com/a/4924494/201648
You can use #Html.ValidationSummary:
#Html.ValidationSummary(true, "Please fill mandatory fields")
If you also want to include a list of property specific errors use:
#Html.ValidationSummary(false, "Please fill mandatory fields")

MVC Controller Error Messages

In MVC 3 app I have a few conditional elements in the controller. for example I have a number say "10" which has met the model state requirements but I have a if statement that checks if the number "10" exists in another table. Should it exist the data is submited but should it not exist I return the view and would like to return a error message.
My question is what would be the best way of displaying a error in this situation. I have looked at returning a viewbag message but I would like to style the error message with a box and by adding this style to the view it always gets displayed which is a problem.
You could add the error message to the modelstate:
ModelState.AddModelError("somekey", "some error message");
and inside your view display error messages using the validation summary helper:
#Html.ValidationSummary(false)
You could of course add a string property to your view model and set its value in case of error. Then inside the view check whether the model property has a value and if it does display the error messages inside a custom styled element. It seems a bit like a wheel reinvention assuming you could simply append the error message to the modelstate but worth mentioning.

Easiest way to alter required validation message site wide

I would like to replace validation message for all model properties that have [Required] attribute from the default "The XY field is required." to "*". I want to do that site wide in one place without having to add a custom message to Required attributes or passing additional parameter to ValidationMessageFor() Html helper.
Any ideas?
See the answer to my question here :
Localizing Error Messages In ASP.NET MVC 2 (Default Validation Attributes)

MVC Validation Error Messages not hardcoded in Attributes

I have a requirement that the validation error messages that are passed to my view come from the database.
I want to use data annotation on my model but the error message key in validation attributes can only be a string or a constant.
I'm open to other solutions for validation but I need to pull the error messages from the database.
Are there any other ellegent solutions for validation were the error message is not a string? Right now I am thinking about using T4 to generate a constants file...
My specific problem can be solved by using the ASP.NET MVC2 Metadata and Model Validation Providers.
Here are the articles that got me started:
http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-2-modelmetadata.html
http://dotnetslackers.com/articles/aspnet/customizing-asp-net-mvc-2-metadata-and-validation.aspx
http://haacked.com/archive/2009/10/01/asp.net-mvc-preview-2-released.aspx
You could overload the validation runner to swap out error messages with ones from a database.

Resources