Some enhancements and additions to DataAnnotations, incluiding implemented of a few...
[mono.git] / mcs / class / System.ComponentModel.DataAnnotations / System.ComponentModel.DataAnnotations / StringLengthAttribute.cs
index 4274c9f8c941c5de1f7706149a1c534575a9adfa..f13866ef909c9d363d408a9c99717374f0da1e4f 100644 (file)
@@ -3,8 +3,9 @@
 //
 // Author:
 //     Atsushi Enomoto <atsushi@ximian.com>
+//     Marek Habersack <grendel@twistedcode.net>
 //
-// Copyright (C) 2008 Novell Inc. http://novell.com
+// Copyright (C) 2008-2010 Novell Inc. http://novell.com
 //
 
 //
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 using System;
+using System.Globalization;
 using System.ComponentModel;
 
 namespace System.ComponentModel.DataAnnotations
 {
+#if NET_4_0
+       [AttributeUsage (AttributeTargets.Parameter|AttributeTargets.Property|AttributeTargets.Field, AllowMultiple = false)]
+#else
        [AttributeUsage (AttributeTargets.Property|AttributeTargets.Field, AllowMultiple = false)]
+#endif
        public class StringLengthAttribute : ValidationAttribute
        {
+               public int MaximumLength { get; private set; }
+#if NET_4_0
+               public int MinimumLength { get; set; }
+#endif
                public StringLengthAttribute (int maximumLength)
+                       : base (GetDefaultErrorMessage)
                {
+#if !NET_4_0
+                       if (maximumLength < 0)
+                               throw new ArgumentOutOfRangeException ("maximumLength", String.Format ("Actual value was {0}", maximumLength));
+#endif
                        MaximumLength = maximumLength;
                }
 
-               public int MaximumLength { get; private set; }
+               static string GetDefaultErrorMessage ()
+               {
+                       return "The field {0} must be a string with a maximum length of {1}.";
+               }
 
-               [MonoTODO]
                public override string FormatErrorMessage (string name)
                {
-                       throw new NotImplementedException ();
+#if NET_4_0
+                       return String.Format (CultureInfo.CurrentCulture, ErrorMessageString, name, MaximumLength, MinimumLength);
+#else
+                       return String.Format (CultureInfo.CurrentCulture, ErrorMessageString, name, MaximumLength);
+#endif
                }
 
-               [MonoTODO]
                public override bool IsValid (object value)
                {
-                       throw new NotImplementedException ();
+                       if (value == null)
+                               return true;
+
+                       string str = (string)value;
+                       int max = MaximumLength;
+#if NET_4_0
+                       int min = MinimumLength;
+
+                       // LAMESPEC: documented to throw ArgumentOutOfRangeException
+                       if (max < 0)
+                               throw new InvalidOperationException ("The maximum length must be a nonnegative integer.");
+
+                       if (min > max)
+                               throw new InvalidOperationException (
+                                       String.Format ("The maximum value '{0}' must be greater than or equal to the minimum value '{1}'.",
+                                                      max, min)
+                               );
+
+                       int len = str.Length;
+                       return len <= max && len >= min;
+#else                  
+                       return str.Length <= max;
+#endif
                }
        }
 }