CSLA Validation Rules to client side validation generator?

CSLA Validation Rules to client side validation generator?

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


shrage posted on Thursday, November 12, 2009

It seems like recreating asp.net validation control is a duplicate work when all the logic are already implemented in the business class, as csla validation rules. is there a pattern how to quickly hook on client side validation to a business classes rules collection?

RockfordLhotka replied on Friday, November 13, 2009

ASP.NET MVC 2 (in .NET 4.0) is able to project some of the common validation attributes from DataAnnotations into the client-side view by using jQuery. Since CSLA .NET 3.8 and higher supports DataAnnotations, this means that you can use MVC2 to automatically get the basic validation rules (required, range, regex, etc) projected into your HTML.

I think there are some open source projects along that line for ASP.NET MVC 1 as well.

I don't know that there are equivalent efforts for Web Forms, though I haven't really looked. Projecting elements into a Web Forms UI is harder than with MVC, so perhaps that's why the idea hasn't risen to the forefront.

shrage replied on Tuesday, November 17, 2009

I'm creating an asp.net control for web forms that you can pass in any web control, and it will create client side validation using jquer validator, in order to accomplish that i came up with a 3 types of validation's
DataRule (IsRequired,MinLength,Maxlength etc) - these are rules that describe the data type of a simple value
DataFormat - This is a regular expression that will the describe the format of the expected input in a higher business level, such as Email, Phone etc.
Custom Rules - These are rules which are based on more complex logic.

The first 2 types of rules can be represented in client side validation, the third type of rule we would usually run an ajax method back to the server and let the validation happen there.

So i need a way to loop trough all RuleMethods added for a given property and i will determine for each method what type of method this is and based on that i willl generate validation scripts on the client side.

What is the safest way to read all RuleMethods of a given csla object, i see that each csla class has 3 private ValidationManagers, for instance rules, static rules, and one manage with a combined list, i'm a little bit confused with all the private methods related to get a list of rules method for a property, which one of these methods do i need to make public in order for me to be able to get that list?

DVR replied on Saturday, September 04, 2010

Hi Shrage,

I am trying to do client side validation using the csla validation rules. We are using ASP.NET 3.5 web forms and CSLA 3.8. Looks like you have implemented something similar, you would be able to point me in the right direction or post some code that I can use.

Thanks,

DVR

 

shrage replied on Thursday, November 19, 2009

Responding to your mentioning of MVC and validation, i have noticed that using the mvc's UpdateModel(cslaobject)

if there is a validation error the update model will automatically insert the borken rules in its datamodel state, how does theupdatemodel know how csla works?

RockfordLhotka replied on Saturday, November 21, 2009

You can ask any CSLA editable object for its list of rules. You'll get back a string array containing rule:// URI values, each one describing a rule. You can then use the RuleDescription class (like System.Uri) to easily work with those URI values.

RockfordLhotka replied on Saturday, November 21, 2009

shrage:
Responding to your mentioning of MVC and validation, i have noticed that using the mvc's UpdateModel(cslaobject) if there is a validation error the update model will automatically insert the borken rules in its datamodel state, how does theupdatemodel know how csla works?

CSLA .NET 3.8 includes a Csla.Web.Mvc project that contains a CslaModelBinder. This model binder understands CSLA .NET validation rules and should be used when binding to CSLA business objects.

In CSLA .NET 4.0 the CslaModelBinder will also understand how to create a business object, enabling the full MVC 2 functionality around binding.

Copyright (c) Marimer LLC