Reputation: 8004
Friends,
This problem is really annoying me from last 2 days. I've finally decided to put it on SO.
I have following property in my Model
[Required]
[StringLength(20, MinimumLength = 4, ErrorMessage = "Length of this field should be between 3 and 20.")]
public string Password { get; set; }
[Required]
[StringLength(20, MinimumLength = 4, ErrorMessage = "Password Lenght should be between 4 and 20.")]
[Compare("Password", ErrorMessage = "Password did not match")]
[Display(Name = "Retype Password")]
public string RePassword { get; set; }
And Following HTML in razor to create input fields for the same.
<div class="form-group">
@Html.LabelFor(m => m.Password, new { @class = "control-label col-md-4" })
<div class="col-md-4">
@Html.PasswordFor(m => m.Password, new { @class = "form-control", placeholder = "Password" })
</div>
<div class="col-md-4">
@Html.ValidationMessageFor(m => m.Password)
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.RePassword, new { @class = "control-label col-md-4" })
<div class="col-md-4">
@Html.PasswordFor(m => m.RePassword, new { @class = "form-control", placeholder = "Retype Password" })
</div>
<div class="col-md-4">
@Html.ValidationMessageFor(m => m.RePassword)
</div>
</div>
It always render following HTML
<div class="form-group">
<label class="control-label col-md-4" for="Password">
Password
</label>
<div class="col-md-4">
<input id="Password" class="form-control" type="password" placeholder="Password" name="Password" data-val-required="The Password field is required." data-val-length-min="4" data-val-length-max="20" data-val-length="Length of this field should be between 3 and 20." data-val="true"></input>
</div>
<div class="col-md-4">
<span class="field-validation-valid help-block" data-valmsg-replace="true" data-valmsg-for="Password"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" for="RePassword">
Retype Password
</label>
<div class="col-md-4">
<input id="RePassword" class="form-control" type="password" placeholder="Retype Password" name="RePassword" data-val-required="The Retype Password field is required." data-val-length-min="4" data-val-length-max="20" data-val-length="Password Lenght should be between 4 and 20." data-val-equalto-other="*.Password" data-val-equalto="'Retype Password' and 'Password' do not match." data-val="true"></input>
</div>
<div class="col-md-4">
<span class="field-validation-valid help-block" data-valmsg-replace="true" data-valmsg-for="RePassword"></span>
</div>
</div>
Its No matter what I write in ErrorMessage
in Compare
attribute, It always has same value for data-val-equalto
attribute that is 'Retype Password' and 'Password' do not match.
for RePassword
filed
Upvotes: 0
Views: 2370
Reputation: 1148
This answer is very late so I'm sure you already figured this out, and since I didn't find any solution online that answered this question directly, I decided share what I did for the benefit of those searching for a solution:
Create a Custom Compare Attribute and apply your own error to it.
public class CustomCompareAttribute : CompareAttribute
{
public CustomCompareAttribute(string otherProperty, string ErrorCode) : base(otherProperty)
{
ErrorMessage = CustomMessage(ErrorCode);
}
private static string CustomMessage(string ErrorCode)
{
string ErrorMsg = ErrorCode;
//ErrorMsg = Translate.Item(ErrorCode); <-- My logic from database
return ErrorMsg;
}
}
Then in your Model you simply add your custom annotation (leaving out the word "Attribute"):
[Required]
[StringLength(20, MinimumLength = 4, ErrorMessage = "Password Lenght should be between 4 and 20.")]
[CustomCompare("Password", "My own message: Passwords do not match!")] // <-- Use your new Custom Attribute here
[Display(Name = "Retype Password")]
public string RePassword { get; set; }
Upvotes: 1
Reputation: 1184
this is a bug :CompareAttribute does not use custom error messages and already fixed
download the fixed CompareAttributeAdapter http://aspnetwebstack.codeplex.com/Download/AttachmentDownload.ashx?ProjectName=aspnetwebstack&WorkItemId=1401&FileAttachmentId=755657 and RegisterAdapter
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(System.ComponentModel.DataAnnotations.CompareAttribute), typeof(TheFixed.CompareAttributeAdapter));
Upvotes: 2