Quantcast
Channel: ASPNETWebStack Issue Tracker Rss Feed
Viewing all articles
Browse latest Browse all 7215

Closed Unassigned: CompareAttribute ErrorMessage is not working in MVC 5? [1463]

$
0
0
Currently there is a bug in ASP.NET MVC 5 which is not taking care of System.ComponentModel.DataAnnotations.CompareAttribute.ErrorMessage. When I dive into source, I found that CompareAttributeWrapper.FormatErrorMessage is not using attribute.ErrorMessage,

```
private sealed class CompareAttributeWrapper : DataAnnotationsCompareAttribute
{
private readonly string _otherPropertyDisplayName;

public CompareAttributeWrapper(DataAnnotationsCompareAttribute attribute, ModelMetadata metadata)
: base(attribute.OtherProperty)
{
_otherPropertyDisplayName = attribute.OtherPropertyDisplayName;
if (_otherPropertyDisplayName == null && metadata.ContainerType != null)
{
_otherPropertyDisplayName = ModelMetadataProviders.Current.GetMetadataForProperty(() => metadata.Model, metadata.ContainerType, attribute.OtherProperty).GetDisplayName();
}
}

public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentCulture, ErrorMessageString, name, _otherPropertyDisplayName ?? OtherProperty);
}
}
```

You can easily reproduce it using,
```
[Compare("Password", ErrorMessage = "The password & confirmation password are not matched.")]
```
In this case the ErrorMessage will never used. The quick fix will be to use obselete System.Web.Mvc.CompareAttribute,

```
[System.Web.Mvc.Compare("Password", ErrorMessage = "The password & confirmation password are not matched.")]
```

Comments: fixed by: https://aspnetwebstack.codeplex.com/workitem/1401

Viewing all articles
Browse latest Browse all 7215

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>