StringMinLength

StringMinLength

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


DeHaynes posted on Wednesday, September 20, 2006

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)
{
   
int min = ((MinLengthRuleArgs)e).MinLength;

   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;
   }
}

public class MinLengthRuleArgs : RuleArgs
{
   
private int _minLength;
   
/// <summary>
   
/// Get the min length for the string.
   
/// </summary>

   public int MinLength
   {
      
get { return _minLength; }
   }

   /// <summary>
   
/// Create a new object.
   
/// </summary>
   
/// <param name="propertyName">Name of the property to validate.</param>
   
/// <param name="minLength">Min length of characters allowed.</param>
   
public MinLengthRuleArgs(string propertyName, int minLength) : base(propertyName)
   {
      _minLength = minLength;
   }
   
/// <summary>
   
/// Return a string representation of the object.
   
/// </summary>

   public override string ToString()
   {
      
return base.ToString() + "!" + _minLength.ToString();
   }
}

#endregion

DeHaynes replied on Wednesday, September 20, 2006

You also need to add a description of the rule in the resources, for when there is an error.  The name of the rule should be "StringMinLengthRule".  For mine I used "{0} must be at least {1} characters".

JoeFallon1 replied on Wednesday, September 20, 2006

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 Function

Joe

DeHaynes replied on Wednesday, September 20, 2006

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 Function

Joe

You are talking about the validation rules?

JoeFallon1 replied on Wednesday, September 20, 2006

Yes. This code uses the old style.

I recommend using the newer style for RuleArgs.

public override string ToString()
   {
      
return base.ToString() + "!" + _minLength.ToString();
   }
}

RockfordLhotka replied on Wednesday, September 20, 2006

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 Smile [:)]

RockfordLhotka replied on Wednesday, September 20, 2006

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