Custom validation attribute to be used in CSLA objects

Custom validation attribute to be used in CSLA objects

Old forum URL: forums.lhotka.net/forums/t/11784.aspx


correodemarques posted on Friday, January 11, 2013

I know how to create custom validation attributes and I know how to make ASP.MVC client validation work with those attributes if I implement IClientValidatable interface, but then I would need to reference System.Web.MVC in the library containing my CSLA objects.

Is it possible to create a custom validation attribute that will be recognized by MVC without having to implement IClientValidatable?

Something that works as System.ComponentModel.DataAnnotations.RequiredAttribute and that can be applied to CSLA properties without referencing System.Web.MVC.

JonnyBee replied on Sunday, January 13, 2013

Any custom validation attribute that inherits from System.ComponentModel.DataAnnotations.ValidationAttribute will be used by the CSLA rule engine - just as the RequiredAttribute is.

See this excercise on MSDN on how to create custom validation rules and plugin to ASP.NET MVC 3:
http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvccustomvalidation.aspx  

correodemarques replied on Monday, January 14, 2013

Hi Jonny,

Thanks for your answer.

Yes, I already know how to create an attribute that inherit from ValidationAttribute and is recognized by CSLA.

My question is about how to make that attribute in a way that is recognized by MVC to generate the HTML needed for the unobtrusive validation to work, without having to reference System.Web.MVC in my class library.

If I follow the tutorial I would have to implement IClientValidatable and then if I try to apply my attribute to a property, I will need to reference System.Web.MVC in my class library. At least that is what I understood.

RockfordLhotka replied on Monday, January 14, 2013

This sounds like a question you might need to ask in an ASP.NET forum on MSDN or something. I've never explored this part of the DataAnnotations infrastructure.

Copyright (c) Marimer LLC