From d22d6793ba50a0c0a087788e7b59d5f3b1732769 Mon Sep 17 00:00:00 2001 From: ryanbrandenburg Date: Wed, 25 Nov 2015 11:06:30 -0800 Subject: [PATCH] * Include DisplayName in errorMessage --- .../DataAnnotationsClientModelValidatorOfTAttribute.cs | 6 ++++-- .../DataAnnotationsModelValidator.cs | 3 ++- .../DataAnnotationsModelValidatorTest.cs | 2 +- .../MaxLengthAttributeAdapterTest.cs | 5 +++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.DataAnnotations/DataAnnotationsClientModelValidatorOfTAttribute.cs b/src/Microsoft.AspNet.Mvc.DataAnnotations/DataAnnotationsClientModelValidatorOfTAttribute.cs index 130142df57..ecfa32413d 100644 --- a/src/Microsoft.AspNet.Mvc.DataAnnotations/DataAnnotationsClientModelValidatorOfTAttribute.cs +++ b/src/Microsoft.AspNet.Mvc.DataAnnotations/DataAnnotationsClientModelValidatorOfTAttribute.cs @@ -52,15 +52,17 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation throw new ArgumentNullException(nameof(modelMetadata)); } + var displayName = modelMetadata.GetDisplayName(); + if (_stringLocalizer != null && !string.IsNullOrEmpty(Attribute.ErrorMessage) && string.IsNullOrEmpty(Attribute.ErrorMessageResourceName) && Attribute.ErrorMessageResourceType == null) { - return _stringLocalizer[Attribute.ErrorMessage]; + return _stringLocalizer[Attribute.ErrorMessage, displayName]; } - return Attribute.FormatErrorMessage(modelMetadata.GetDisplayName()); + return Attribute.FormatErrorMessage(displayName); } } } diff --git a/src/Microsoft.AspNet.Mvc.DataAnnotations/DataAnnotationsModelValidator.cs b/src/Microsoft.AspNet.Mvc.DataAnnotations/DataAnnotationsModelValidator.cs index b4c73c0455..c67f656507 100644 --- a/src/Microsoft.AspNet.Mvc.DataAnnotations/DataAnnotationsModelValidator.cs +++ b/src/Microsoft.AspNet.Mvc.DataAnnotations/DataAnnotationsModelValidator.cs @@ -61,7 +61,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation string.IsNullOrEmpty(Attribute.ErrorMessageResourceName) && Attribute.ErrorMessageResourceType == null) { - errorMessage = _stringLocalizer[Attribute.ErrorMessage]; + var displayName = validationContext.Metadata.GetDisplayName(); + errorMessage = _stringLocalizer[Attribute.ErrorMessage, displayName]; } var validationResult = new ModelValidationResult(errorMemberName, errorMessage ?? result.ErrorMessage); diff --git a/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/DataAnnotationsModelValidatorTest.cs b/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/DataAnnotationsModelValidatorTest.cs index 4200b50029..2e5901bdae 100644 --- a/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/DataAnnotationsModelValidatorTest.cs +++ b/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/DataAnnotationsModelValidatorTest.cs @@ -268,7 +268,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation var localizedString = new LocalizedString("Length", "Longueur est invalide"); var stringLocalizer = new Mock(); - stringLocalizer.Setup(s => s["Length"]).Returns(localizedString); + stringLocalizer.Setup(s => s["Length", It.IsAny()]).Returns(localizedString); var validator = new DataAnnotationsModelValidator(attribute.Object, stringLocalizer.Object); var validationContext = new ModelValidationContext() diff --git a/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/MaxLengthAttributeAdapterTest.cs b/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/MaxLengthAttributeAdapterTest.cs index f87506fc20..5c13569275 100644 --- a/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/MaxLengthAttributeAdapterTest.cs +++ b/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/MaxLengthAttributeAdapterTest.cs @@ -77,7 +77,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation attribute.ErrorMessage = errorKey; var localizedString = new LocalizedString(errorKey, "Longueur est invalide"); var stringLocalizer = new Mock(); - stringLocalizer.Setup(s => s[errorKey]).Returns(localizedString); + stringLocalizer.Setup(s => s[errorKey, It.IsAny()]).Returns(localizedString); + var adapter = new MaxLengthAttributeAdapter(attribute, stringLocalizer.Object); var actionContext = new ActionContext(); @@ -94,4 +95,4 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation Assert.Equal("Longueur est invalide", rule.ErrorMessage); } } -} \ No newline at end of file +}