StringMinLength should not behave like IsRequired

StringMinLength should not behave like IsRequired

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


shrage posted on Monday, December 07, 2009

Why is it when add a string MinLength rule, and i don't set any value to that property this rule is considered broken, i would think that a MinLength rule is only checking if do set a value that i should match the minlength but if i dont set anything to the value and i dont have an IsRequired rule the property should be valid.

RockfordLhotka replied on Tuesday, December 08, 2009

This is why the business rule subsystem is designed to allow people to write their own rules. The idea that any one set of common rules will meet everyone's needs is unrealistic, so when a rule doesn't meet your needs you should feel free to write your own rule methods in your own rule library.

shrage replied on Tuesday, December 08, 2009

If i might suggest, a simple IsRequired flag to the RuleArgs base class, and all common rules to use this flag to know how to treat empty values. This piece of information makes sense to all rules.

rsbaker0 replied on Tuesday, December 08, 2009

It's too easy to just write this yourself. You can even call the existing CSLA rule. Start your own rule collection with this one: :)


public static bool NonEmptyStringMinLength(object target, RuleArgs e)
{
string value = (string)Utilities.CallByName(
target, e.PropertyName, CallType.Get);
return string.IsNullOrEmpty(value) || Csla.Validation.CommonRules.StringMinLength(target, e);
}

shrage replied on Tuesday, December 08, 2009

Yea you are right, at least i know that's by design and not that i did something wrong ;-)

Copyright (c) Marimer LLC