MVC Validation Error Messages not hardcoded in Attributes - asp.net-mvc

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.

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.

Display errors using Knockout JS + MVC + Server-side Model Validation?

Html form is controlled using Knockout JS and jQuery templates.
Basic jQuery validation is in use to validate fields.
Form gets serialized to JSON and submitted to MVC controller action using AJAX.
MVC controller action performs server-side model validation, adds errors to ModelState.
What's the best practice to return those errors to the client - iterating through errors in ModelState and adding them to key/value collection of errors in the JSON response?
How do you display errors on the client? How do you 'bind' the key/value collection of errors to relevant fields on the model?
Say there is a "name" field on the model, with a corresponding textbox rendered by the jQuery template. How does one take the error for the "name" field in the collection of errors and display the error message beneath the "name" textbox?
There's two validation plugins for ko.js (found here) that could help you,
Knock-Knock validation
Knockout Validation
You can wire one of those to the mvc unobstrsive validation data injected client side.
If you are using MVC, unobtrusive javascript performs client side validation based on the validation set in your model. You need not perform any additional configuration.
Having said that, there is no direct way to perform client side validation based on the model using javascript and knockoutjs.
There are couple of ways of doing it on the client side.
Jquery or any other validation frameworks can perform validation. But you need to have tag. Advantage with this approach is your code will be simple and easy to maintain.
You can perform client side custom validation using javascript and bind the validation messages using knockout. This requires you to create error labels for each of the input variable. Advantage with this approach is you will have complete control over how and what has to be displayed.
Personally, I had similar requirement in one of the recent projects and I achieved it using custom validation checks and error labels.

With an MVC application should my business rule validation be replicated in the view model and/or controller?

My MVC application has a well defined domain Model with each Model performing its own business rule checks.
My question is should the business rules be replicated in the view model or controller too or should I just allow the model to generate the validation errors?
Lets assume that the validation we are talking about can't be done client-side and is more complicated than simple field validation that can be done by adding validation attributes to the view model properties.
The problem with allowing the model to handle all of the validation is that the error messages it generates might not be suitable for the particular view that its coupled to, field names might not be correct for example. Also I need to add the error to the ModelState using the view model property name not the property name from the domain model.
The problem with adding the same business rule validation to the viewmodel/controller is the obvious duplication maintenance issue and it means the validation in my domain model really should never produce any errors which kind of makes it a bit pointless.
How do people usually handle this?
The solution I typically used to use in this scenario is to have the validation in the Model (or in my case typically a validation library) but add the error in the controller which allows you to catch the standard validation error like this:
public ActionResult Submit(String email)
{
string errorMessage = "";
if(Validation.IsValidEmail(email, out errorMessage))
{
ViewData.AddModelError("EmailAddress", "My Custom Error Message");
//or
ViewData.AddModelError("EmailAddress", errorMessage);
}
}
This may not be exactly what you are looking for but it might help you come up with a way of maximising re-usable code whilst still being able to customise it.
I used to do it like this - but I bit the bullet and my most recent applications use standard error messages everywhere - my motivation for this is that users are increasingly comfortable with short-simple validation message and field names are not required in the message if the message is displayed inline with the form field rather than as a summary. This also allows me to put all my rules/messages in data annotations and I find that it works a treat.

Changing error message for datatype validation?

I've a small question on ASP.NET MVC 2. I'm using Data Annotations on my Presentation Model and its model binder per default. But one step back, I want to do the most elementary thing: validation of the data type.
Let's say I have an "int ID" on my model and want to edit the according entity in my view. If I enter something wrong like "foo", then I get a model error back, but the validation message is on English. I want to have this message in other languages, too. How can I do this?
I remember that in MVC 1 I've written a custom model binder which checks the data types, but that's not what I want to do (at least not in MVC 2). Thus I hope there is a better way...
Thanks, Matthias
Model Validation with Localization
http://helios.ca/2010/02/17/asp-net-mvc-2-model-validation-with-localization/
Look for solution at the end of this page:
http://jwwishart.wordpress.com/2010/03/22/custom-server-and-client-side-required-validator-in-mvc-2-using-jquery-validate/
I checked this in my MVC 3 RTM project and it works well.

ASP.NET MVC - Model Validation regarding its dataType Length

I'm using ASP.NET MVC 1 on a project where I would like to validate my form submission with the ModelBinding helpers, actually I am validating it with ModelState.IsValid but it doesn't seem to consider the dataType length when validating it. For example:
I have a field nvarchar(70) called Name , but when in my form I submit a name with 200 chars, it pass the validation anyways...anyone knows how to prevent this to happen and make the modelBinding respect the datatype length?
Thanks in advance
Update: Thanks for the help till now: I'm using just standard Linq associations and modelBinding to validate the models, I hope this can help somehow
Cheers
This is a excellent comparison from the validation alternatives -so you can select what is better for you ; )
Includes concrete implementations.
I recommend you to keep your model validation logic out of the binding module. and just feed your ModelState with the validation errors from inside your service layer.
See this series from the ASP.Net MVC page for examples : )

Resources