Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a question, When I apply a validation attribute (which is having associated validator to it) twice on a property, for eg I have number1 which should be lesser than number2 and number3. so I apply something like below.

[LessThan("Number2",ErrorMessage="Error Message1")]
[LessThan("Number3",ErrorMessage="Error Message2")]
public int Number1

problem here is when it evaluates Number1 < Number3 to true, then it is not checking for Number1 <Number2, it assumes that validation for the whole field is passed. How can we ensure, we go through each validation (attribute rule) and make sure the correct Validation error is returned.
Posted
Comments
Salmen Essridi 17-Jul-11 11:23am    
It's MVC2 or MVC3 ?

I found a way to do it, using reflection, reading all the attributes applied on a property in the validator. I can post the code if any body is looking for the same problem.
 
Share this answer
 
also another solution, there is a TypeID in Property in Attribute class, which we can override in the derived class and return new object() in the get property which solves the problem.
 
Share this answer
 
It's a very good question, i faced this problem.
So the smart solution was by doing a custom Data annotations.

try this links to find out.

http://byatool.com/mvc/custom-data-annotations-with-mvc-how-to-check-multiple-properties-at-one-time/[^]

http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx[^]


I hope that helps. Good luck.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900