[RESOLVED]Data annotation validation

Hi
I have two questions regarding the data annotation validation.

1. Can you change the look og how the error message i show. Maybe use some of my own html
2. Can i use data annotation validation with viewmodels? I know that i can put data annotation on the attributes of my viewmodels but is it the way to go?

Hope someone can help
Jakobjensen

Yes, in Viewmodels you can implement the INotifyableObject interface.  MVC will call this implementation just before the post back enters the controller to validate anything you want. There are two modes to validation 1) For the Models itself using data
annotations and 2) And any custom business rules you need to implement in the View model.  To notify the view, the ViewModel validate method will return a collection of <ViewResult>  if you use the "Yield return new ViewResults("blah, blah blah")" construct
you can write as many validation rules as you need an very easily send them to the view.  

Reminder, make sure you set up something like @Html.ValidationSummry(true or false) up in the view in order to see them.  One final note, any Action method checking for Model.ViewState is valid any bypassing logic, will force those errors to be fixed!  In
other words it is a controller decision on what to do, however, why set up exception parsing rules for this if you just need to warn the user of something rather than forcing a correction.  For warning messages you are better off implementing on client side.
 Just create a hidden field or dialog that pops up from using JQuery or Javascript for those.

Can you change the look?  Anything is possible if you implement the entire system yourself or use JavaQuery to manipulate the DOM after document.ready.  You’ll easily be able to spot what MVC does to generate those messages.  Jquery can do anything with
them. 

Thanks for the answer.

So the way i do now is that i have some homemade javascript to handle client side validation.
And then i want to use entity frameworks serverside validation.
How do i make entity framework talk with my javascript.
I want the same error to show up both for client side errors and for server side errors

Im not sure i made myself clear…
I want to validate with the annotation from my business classes.
I have viewmodels.
And i have some javascript that validates and thats fine by me. But i dont know how to handle to kind of validation..

Lets say the user enters something wrong. Then he see my error message from javascript.

But if he has javascript disabled he should see the message from entity framework.

How do you handle these to validation?

Hi Jakobjensen,

jakobjensen

Can you change the look og how the error message i show. Maybe use some of my own html

I suggest that you could specify the style in the parent element, such as:

<div style="..."> 
@Html.ValidationMessageFor(m => m.UserName)
</div>

You also could custom HtmlHelper to custom it.

# Custom ValidationSummary template Asp.net MVC

http://stackoverflow.com/questions/5857851/custom-validationsummary-template-asp-net-mvc-3

jakobjensen

Can i use data annotation validation with viewmodels? I know that i can put data annotation on the attributes of my viewmodels but is it the way to go?

You just need add the data annotation validation attribute to the fields or properties of the
ViewModels.

Best Regards

Starain

Hi,

1. Use CSS style, what you want.

2. Custom Data Annotation always good, beco’z you can push your logic into validation.

http://20fingers2brains.blogspot.com/2013/04/custom-annotations-in-aspnet-mvc3-razor.html

Thanks,

Jai.

Leave a Reply