* Include DisplayName in errorMessage

This commit is contained in:
ryanbrandenburg 2015-11-25 11:06:30 -08:00
parent c5346f7bf9
commit d22d6793ba
4 changed files with 10 additions and 6 deletions

View File

@ -52,15 +52,17 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
throw new ArgumentNullException(nameof(modelMetadata)); throw new ArgumentNullException(nameof(modelMetadata));
} }
var displayName = modelMetadata.GetDisplayName();
if (_stringLocalizer != null && if (_stringLocalizer != null &&
!string.IsNullOrEmpty(Attribute.ErrorMessage) && !string.IsNullOrEmpty(Attribute.ErrorMessage) &&
string.IsNullOrEmpty(Attribute.ErrorMessageResourceName) && string.IsNullOrEmpty(Attribute.ErrorMessageResourceName) &&
Attribute.ErrorMessageResourceType == null) Attribute.ErrorMessageResourceType == null)
{ {
return _stringLocalizer[Attribute.ErrorMessage]; return _stringLocalizer[Attribute.ErrorMessage, displayName];
} }
return Attribute.FormatErrorMessage(modelMetadata.GetDisplayName()); return Attribute.FormatErrorMessage(displayName);
} }
} }
} }

View File

@ -61,7 +61,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
string.IsNullOrEmpty(Attribute.ErrorMessageResourceName) && string.IsNullOrEmpty(Attribute.ErrorMessageResourceName) &&
Attribute.ErrorMessageResourceType == null) 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); var validationResult = new ModelValidationResult(errorMemberName, errorMessage ?? result.ErrorMessage);

View File

@ -268,7 +268,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
var localizedString = new LocalizedString("Length", "Longueur est invalide"); var localizedString = new LocalizedString("Length", "Longueur est invalide");
var stringLocalizer = new Mock<IStringLocalizer>(); var stringLocalizer = new Mock<IStringLocalizer>();
stringLocalizer.Setup(s => s["Length"]).Returns(localizedString); stringLocalizer.Setup(s => s["Length", It.IsAny<object[]>()]).Returns(localizedString);
var validator = new DataAnnotationsModelValidator(attribute.Object, stringLocalizer.Object); var validator = new DataAnnotationsModelValidator(attribute.Object, stringLocalizer.Object);
var validationContext = new ModelValidationContext() var validationContext = new ModelValidationContext()

View File

@ -77,7 +77,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
attribute.ErrorMessage = errorKey; attribute.ErrorMessage = errorKey;
var localizedString = new LocalizedString(errorKey, "Longueur est invalide"); var localizedString = new LocalizedString(errorKey, "Longueur est invalide");
var stringLocalizer = new Mock<IStringLocalizer>(); var stringLocalizer = new Mock<IStringLocalizer>();
stringLocalizer.Setup(s => s[errorKey]).Returns(localizedString); stringLocalizer.Setup(s => s[errorKey, It.IsAny<object[]>()]).Returns(localizedString);
var adapter = new MaxLengthAttributeAdapter(attribute, stringLocalizer.Object); var adapter = new MaxLengthAttributeAdapter(attribute, stringLocalizer.Object);
var actionContext = new ActionContext(); var actionContext = new ActionContext();
@ -94,4 +95,4 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
Assert.Equal("Longueur est invalide", rule.ErrorMessage); Assert.Equal("Longueur est invalide", rule.ErrorMessage);
} }
} }
} }