Single ValidationRule as property attribute

Single ValidationRule as property attribute

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


saeedsamie posted on Friday, October 23, 2009

Hi everybody,

I searched in the forum for finding how to implement a rule as a property attribute but I couldn't find it. I appreciate if anyone could show me how to do it.

RockfordLhotka replied on Friday, October 23, 2009

Are you talking about the new 3.8 support for DataAnnotation attributes?

Assuming you are using 3.8 Beta 2 (which isn't out yet - but let's say you've pulled the current code out of svn):

  1. Reference System.ComponentModel.DataAnnotations
  2. Add a 'using System.ComponentModel.DataAnnotations;' to your business class
  3. Declare your property like normal, but decorate the property with one of the new validation attributes, such as Required or Range.
  4. Then either:
    1. Don't override AddBusinessRules() at all - the base implementation will add the attribute rules, or
    2. Add a call to 'base.AddBusinessRules();' in your AddBusinessRules() override - so the base implementation can add the attribute rules

That's all there is to it.

saeedsamie replied on Friday, October 23, 2009

Hi Rocky,

I checked exactly the same as you specified but it doesn't work with windowsform and no error shown by errorprovider like when using validation commonrules . Where do you think the problem come from?

Thanks

RockfordLhotka replied on Friday, October 23, 2009

You are using the code directly out of

svn://svn.lhotka.net/csla/core/branches/V3-8-x

that you retrieved using TortoiseSvn or something?

saeedsamie replied on Tuesday, October 27, 2009

I don't know anything about the address you mentioned but I download the framework directly from your site few months ago.

thanks

RockfordLhotka replied on Tuesday, October 27, 2009

The reason I'm confused is that the idea of a validation rule as a property
attribute is something I just added to CSLA .NET. That concept didn't exist
a few months ago.

If you want to use validation attributes you should download CSLA .NET 3.8
Beta 2 (just released), which supports this concept.

Copyright (c) Marimer LLC