I am just wondering why there wasn't a StringMinLength in the Validation.CommonRules?
I added it, here is the code:
#region
StringMinLength[System.Diagnostics.CodeAnalysis.
SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")] public static bool StringMinLength(object target, RuleArgs e) if (min < 0)
min = 0;
string value = (string)Utilities.CallByName(target, e.PropertyName, CallType.Get);
if (min == 0)
{
if (value == null)
{
e.Description = String.Format(Resources.StringMinLengthRule, e.PropertyName, min.ToString());
return false;
}
else
return true;
}
else
{
if (String.IsNullOrEmpty(value) || (value.Length < min))
{
e.Description = String.Format(Resources.StringMinLengthRule, e.PropertyName, min.ToString());
return false;
}
else
return true;
}
}
#endregion
Thanks.
In 2.1 Rocky changes the way to use ToString to code like this:
Public Overrides Function ToString() As String
Return MyBase.ToString & "?minLength=" & _minLength.ToString End FunctionJoe
JoeFallon1:Thanks.
In 2.1 Rocky changes the way to use ToString to code like this:
Public Overrides Function ToString() As String
Return MyBase.ToString & "?minLength=" & _minLength.ToString End FunctionJoe
You are talking about the validation rules?
Yes. This code uses the old style.
I recommend using the newer style for RuleArgs.
public override string ToString()
{
return base.ToString() + "!" + _minLength.ToString();
}
}
Yes, in 2.1 the ToString value is used to build a URI for the rule, so the ToString method needs to return the correct part of the URI as Joe shows in a previous post.
For most people this may not matter, but this goes along with another 2.1 feature: the ability to get a list of all the rules for an object - and that list returns an array of string. Each string is a URI, and you can use System.Uri to parse them to find things like the rule name, property name, parameters, etc.
Obviously you can't use System.Uri if any part of the ToString chain doesn't play nicely
Oh, and to answer the original question - why isn't this rule in CommonRules?
I knew from the start that I couldn't implement every rule out there. This one would make a good addition, no doubt about it.
But my hope is that there'll be an effort on CSLAcontrib to create a community-maintained validation rules library. That would make sense if there are enough commonly used rules that people build and would be willing to donate to such an effort.
Copyright (c) Marimer LLC