From aef73318b5da96a0a2568a63e1bc50a0d4a17040 Mon Sep 17 00:00:00 2001 From: Robb Schiefer Date: Thu, 22 Sep 2011 15:00:59 -0500 Subject: [PATCH 01/10] created an abstract base comparison attribute from which equalto can be derived; also created greaterthan --- .../DataAnnotationsExtensions.Tests.csproj | 2 + .../DataAnnotationsExtensions.csproj | 2 + DataAnnotationsExtensions/EqualToAttribute.cs | 52 +++---------------- .../Resources/ValidatorResources.Designer.cs | 18 +++++++ .../Resources/ValidatorResources.resx | 6 +++ 5 files changed, 34 insertions(+), 46 deletions(-) diff --git a/DataAnnotationsExtensions.Tests/DataAnnotationsExtensions.Tests.csproj b/DataAnnotationsExtensions.Tests/DataAnnotationsExtensions.Tests.csproj index 16bd9fe..8d502a8 100644 --- a/DataAnnotationsExtensions.Tests/DataAnnotationsExtensions.Tests.csproj +++ b/DataAnnotationsExtensions.Tests/DataAnnotationsExtensions.Tests.csproj @@ -59,6 +59,7 @@ + @@ -90,6 +91,7 @@ ResXFileCodeGenerator ErrorResources.Designer.cs + Designer diff --git a/DataAnnotationsExtensions/DataAnnotationsExtensions.csproj b/DataAnnotationsExtensions/DataAnnotationsExtensions.csproj index 08e6efe..15e82cb 100644 --- a/DataAnnotationsExtensions/DataAnnotationsExtensions.csproj +++ b/DataAnnotationsExtensions/DataAnnotationsExtensions.csproj @@ -50,9 +50,11 @@ Properties\CommonAssemblyInfo.cs + + diff --git a/DataAnnotationsExtensions/EqualToAttribute.cs b/DataAnnotationsExtensions/EqualToAttribute.cs index e4c5d02..ab4aef7 100644 --- a/DataAnnotationsExtensions/EqualToAttribute.cs +++ b/DataAnnotationsExtensions/EqualToAttribute.cs @@ -14,58 +14,18 @@ namespace DataAnnotationsExtensions /// From Mvc3 Futures /// [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] - public class EqualToAttribute : ValidationAttribute + public class EqualToAttribute : BaseComparisonAttribute { - public EqualToAttribute(string otherProperty) - { - if (otherProperty == null) - { - throw new ArgumentNullException("otherProperty"); - } - OtherProperty = otherProperty; - OtherPropertyDisplayName = null; - } - - public string OtherProperty { get; private set; } + public EqualToAttribute(string otherProperty) : base(otherProperty) {} - public string OtherPropertyDisplayName { get; set; } - - public override string FormatErrorMessage(string name) + internal override string GetDefaultError() { - if (ErrorMessage == null && ErrorMessageResourceName == null) - { - ErrorMessage = ValidatorResources.CompareAttribute_MustMatch; - } - - var otherPropertyDisplayName = OtherPropertyDisplayName ?? OtherProperty; - - return String.Format(CultureInfo.CurrentCulture, ErrorMessageString, name, otherPropertyDisplayName); + return ValidatorResources.CompareAttribute_MustMatch; } - protected override ValidationResult IsValid(object value, ValidationContext validationContext) + internal override bool Compare(object objA, object objB) { - var memberNames = new[] {validationContext.MemberName}; - - PropertyInfo otherPropertyInfo = validationContext.ObjectType.GetProperty(OtherProperty); - if (otherPropertyInfo == null) - { - return new ValidationResult(String.Format(CultureInfo.CurrentCulture, ValidatorResources.EqualTo_UnknownProperty, OtherProperty), memberNames); - } - - var displayAttribute = - otherPropertyInfo.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault() as DisplayAttribute; - - if (displayAttribute != null && !string.IsNullOrWhiteSpace(displayAttribute.Name)) - { - OtherPropertyDisplayName = displayAttribute.Name; - } - - object otherPropertyValue = otherPropertyInfo.GetValue(validationContext.ObjectInstance, null); - if (!Equals(value, otherPropertyValue)) - { - return new ValidationResult(FormatErrorMessage(validationContext.DisplayName), memberNames); - } - return null; + return Equals(objA, objB); } } } \ No newline at end of file diff --git a/DataAnnotationsExtensions/Resources/ValidatorResources.Designer.cs b/DataAnnotationsExtensions/Resources/ValidatorResources.Designer.cs index 20a1592..9a0af2b 100644 --- a/DataAnnotationsExtensions/Resources/ValidatorResources.Designer.cs +++ b/DataAnnotationsExtensions/Resources/ValidatorResources.Designer.cs @@ -123,6 +123,15 @@ internal static string CommonControls_NameRequired { } } + /// + /// Looks up a localized string similar to '{0}' is not greater than '{1}'.. + /// + internal static string CompareAttribute_MustBeGreater { + get { + return ResourceManager.GetString("CompareAttribute_MustBeGreater", resourceCulture); + } + } + /// /// Looks up a localized string similar to '{0}' and '{1}' do not match.. /// @@ -132,6 +141,15 @@ internal static string CompareAttribute_MustMatch { } } + /// + /// Looks up a localized string similar to '{0}' types can not be compared.. + /// + internal static string CompareAttribute_TypeNotSupported { + get { + return ResourceManager.GetString("CompareAttribute_TypeNotSupported", resourceCulture); + } + } + /// /// Looks up a localized string similar to The IControllerFactory '{0}' did not return a controller for a controller named '{1}'.. /// diff --git a/DataAnnotationsExtensions/Resources/ValidatorResources.resx b/DataAnnotationsExtensions/Resources/ValidatorResources.resx index 7b16ef9..60dc9ee 100644 --- a/DataAnnotationsExtensions/Resources/ValidatorResources.resx +++ b/DataAnnotationsExtensions/Resources/ValidatorResources.resx @@ -291,4 +291,10 @@ The {0} field is not a valid URL. + + '{0}' is not greater than '{1}'. + + + '{0}' types can not be compared. + \ No newline at end of file From 7b15d691dce832c75e9eb77575925aeff3a97089 Mon Sep 17 00:00:00 2001 From: Robb Schiefer Date: Thu, 22 Sep 2011 15:40:45 -0500 Subject: [PATCH 02/10] added greaterthan adapter and rule --- .../Adapters/EqualToAttributeAdapter.cs | 7 ++++--- .../DataAnnotationsExtensions.ClientValidation.csproj | 2 ++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/DataAnnotationsExtensions.ClientValidation/Adapters/EqualToAttributeAdapter.cs b/DataAnnotationsExtensions.ClientValidation/Adapters/EqualToAttributeAdapter.cs index 284ddef..cdec455 100644 --- a/DataAnnotationsExtensions.ClientValidation/Adapters/EqualToAttributeAdapter.cs +++ b/DataAnnotationsExtensions.ClientValidation/Adapters/EqualToAttributeAdapter.cs @@ -2,12 +2,13 @@ using System.Collections.Generic; using System.Web.Mvc; using DataAnnotationsExtensions.ClientValidation.Resources; +using DataAnnotationsExtensions.ClientValidation.Rules; namespace DataAnnotationsExtensions.ClientValidation.Adapters { - public class EqualToAttributeAdapter : DataAnnotationsModelValidator + public class GreaterThanAttributeAdapter : DataAnnotationsModelValidator { - public EqualToAttributeAdapter(ModelMetadata metadata, ControllerContext context, EqualToAttribute attribute) + public GreaterThanAttributeAdapter(ModelMetadata metadata, ControllerContext context, GreaterThanAttribute attribute) : base(metadata, context, attribute) { } @@ -18,7 +19,7 @@ public override IEnumerable GetClientValidationRules( var otherProp = FormatPropertyForClientValidation(Attribute.OtherProperty); //We'll just use the built-in System.Web.Mvc client validation rule - return new[] { new ModelClientValidationEqualToRule(ErrorMessage, otherProp) }; + return new[] { new ModelClientValidationGreaterThanRule(ErrorMessage, otherProp) }; } private string GetOtherPropertyDisplayName() diff --git a/DataAnnotationsExtensions.ClientValidation/DataAnnotationsExtensions.ClientValidation.csproj b/DataAnnotationsExtensions.ClientValidation/DataAnnotationsExtensions.ClientValidation.csproj index 102f2a3..a94f05b 100644 --- a/DataAnnotationsExtensions.ClientValidation/DataAnnotationsExtensions.ClientValidation.csproj +++ b/DataAnnotationsExtensions.ClientValidation/DataAnnotationsExtensions.ClientValidation.csproj @@ -51,6 +51,7 @@ Properties\CommonAssemblyInfo.cs + @@ -69,6 +70,7 @@ True ClientValidationResources.resx + From 1b446ae23eaa171dec623e29461698adc26694fd Mon Sep 17 00:00:00 2001 From: Robb Schiefer Date: Fri, 23 Sep 2011 08:33:16 -0500 Subject: [PATCH 03/10] added jquery validator and unobtrusive scripts; added web tests --- .../DataAnnotationsModelValidatorProviderExtensions.cs | 1 + .../DataAnnotationsExtensions.Core.csproj | 1 + .../DataAnnotationsExtensions.Web.csproj | 4 ++++ DataAnnotationsExtensions.Web/Views/Shared/_Layout.cshtml | 2 ++ 4 files changed, 8 insertions(+) diff --git a/DataAnnotationsExtensions.ClientValidation/DataAnnotationsModelValidatorProviderExtensions.cs b/DataAnnotationsExtensions.ClientValidation/DataAnnotationsModelValidatorProviderExtensions.cs index c1e163a..5b0f431 100644 --- a/DataAnnotationsExtensions.ClientValidation/DataAnnotationsModelValidatorProviderExtensions.cs +++ b/DataAnnotationsExtensions.ClientValidation/DataAnnotationsModelValidatorProviderExtensions.cs @@ -11,6 +11,7 @@ public static void RegisterValidationExtensions() DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(UrlAttribute), typeof(UrlAttributeAdapter)); DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(CreditCardAttribute), typeof(CreditCardAttributeAdapter)); DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(EqualToAttribute), typeof(EqualToAttributeAdapter)); + DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(GreaterThanAttribute), typeof(GreaterThanAttributeAdapter)); DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(FileExtensionsAttribute), typeof(FileExtensionsAttributeAdapter)); DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(NumericAttribute), typeof(NumericAttributeAdapter)); DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(DigitsAttribute), typeof(DigitsAttributeAdapter)); diff --git a/DataAnnotationsExtensions.Core/DataAnnotationsExtensions.Core.csproj b/DataAnnotationsExtensions.Core/DataAnnotationsExtensions.Core.csproj index 434846d..bd79c82 100644 --- a/DataAnnotationsExtensions.Core/DataAnnotationsExtensions.Core.csproj +++ b/DataAnnotationsExtensions.Core/DataAnnotationsExtensions.Core.csproj @@ -44,6 +44,7 @@ Properties\CommonAssemblyInfo.cs + diff --git a/DataAnnotationsExtensions.Web/DataAnnotationsExtensions.Web.csproj b/DataAnnotationsExtensions.Web/DataAnnotationsExtensions.Web.csproj index 8715815..ca5c555 100644 --- a/DataAnnotationsExtensions.Web/DataAnnotationsExtensions.Web.csproj +++ b/DataAnnotationsExtensions.Web/DataAnnotationsExtensions.Web.csproj @@ -61,6 +61,7 @@ Properties\CommonAssemblyInfo.cs + @@ -97,6 +98,8 @@ + + @@ -201,6 +204,7 @@ + diff --git a/DataAnnotationsExtensions.Web/Views/Shared/_Layout.cshtml b/DataAnnotationsExtensions.Web/Views/Shared/_Layout.cshtml index 61723eb..8b57286 100644 --- a/DataAnnotationsExtensions.Web/Views/Shared/_Layout.cshtml +++ b/DataAnnotationsExtensions.Web/Views/Shared/_Layout.cshtml @@ -71,6 +71,8 @@ + +