Easiest way to alter required validation message site wide - asp.net-mvc

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)

Related

Validation Message is shown even there is no error message

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.

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 .

SSN Validation in MVC2

I have a business requirement where in i need to display SSN Field into three separate text boxes and if any one of the textbox is blank and user clicks save, I need to display a common error message on top of the SSN Field saying- "SSN is required field". I have added required field attribute on the view model on all these fields so i am getting three edit messages saying SSN is required. how to present only one edit in this case ?
I am using ASP.NET MVC 2
My view model is like this
[Required]
SSN_FirstThreeDigits
[Required]
SSN_SecondTwoDigits
[Required]
SSN_ThirdFourDigits
Any suggestions
Thanks
Subu
SSNValidator.com now has an API for validating social security numbers. Data elements include whether or not the number was issued, approximate date of issuance, state of issuance and if it was used in a death claim. Go to the site and click the API/Batch Processing links for details.
The Mvc.ValidationTookit Alpha Release: Conditional Validation with MVC 3 can do this.
You may need a custom model binder. Please have a look at this article and see how he validates date of birth

Clientside validation attributes on hidden,radio, dropdown elements by default in asp.net mvc 3

I'm using ASP.NET MVC 3 + FLUENT VALIDATION + NINJECT
I have model X and rules for that model are something like this:
RuleFor(c => c.FirstName).NotEmpty();
RuleFor(c => c.LastName).NotEmpty();
I spotted a little strange thing, the engine puts validation attributes on all hidden elements and on dropdowns, radio buttons, etc.., and I didn't specified this in the validation configuration for that model, so I guess it is by default...
<input type="hidden" value="1" name="Id" id="Id" data-val-required="&#39;Id&#39; must not be empty." data-val-number="The field Id must be a number." data-val="true">
Validation works because hidden element always have a value, but I have a problem with radio buttons. For example, if I don't want one radio button always to be selected by default but empty and if I want to put validation rules on that item, the rendering puts default validation attributes and on top of my rules, so it's getting messed up and validation doesn't work properly...
Anyone had similar issues or knows about this, or do I have to pull the ASP.NET MVC source and look it up by myself? :)
Semi-Lazy and little-pushed-down-by-deadlines coder
Edit:
I tried proposed solution from this link:
Fluent Validations. Error: Validation type names in unobtrusive client validation rules must be unique
but asp.net mvc emits required attributes on each field regardless of AddImplicitRequiredAttribute settings...
Make the Id property on your view model a nullable integer.
So instead of:
public int Id { get; set; }
you should use:
public int? Id { get; set; }
Same stands true for any other value types that you don't want to be required. You should use nullable types.
Html helpers automatically emit data-val attributes for all non-nullable types which is by design and if you do not want this behavior you will have to write your own custom HTML helpers to generate your input fields and dropdowns. You can no longer rely on TextBoxFor and DropDownListFor helpers as that's how they are designed.

ASP.NET MVC - Replace label with validation error message?

I'd like to highlight field names instead of showing a separate error message when there is a validation error.
Is there any convenient way of doing this other than checking the ModelState Errors collection and wrapping each .LabelFor() in an if?
Also, I'd like to format labels as either bold, or add an asterisk if the model metadata has a [Required] attribute.
You'll need use a custom Object.ascx file by creating either /Shared/DisplayTemplates/Object.ascx or /Shared/EditorTemplates/Object.ascx
I can't answer any better than this guide at Brad Wilson's blog: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html

Resources