Verify mvc3 validation if property values โ€‹โ€‹differ

in MVC3, you can add model validation to check if the properties match as follows:

public string NewPassword { get; set; } [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")] public string ConfirmPassword { get; set; } 

Is there a way to verify that the two properties are different, as in the following make-up code?

 [CheckPropertiesDiffer("OldPassword", ErrorMessage = "Old and new passwords cannot be the same")] public string OldPassword { get; set; } public string ConfirmPassword { get; set; } 
+4
source share
4 answers

Here is what you could use in the model:

 public string OldPassword [NotEqualTo("OldPassword", ErrorMessage = "Old and new passwords cannot be the same.")] public string NewPassword { get; set; } 

And then define the following custom attribute:

 public class NotEqualToAttribute : ValidationAttribute { private const string defaultErrorMessage = "{0} cannot be the same as {1}."; private string otherProperty; public NotEqualToAttribute(string otherProperty) : base(defaultErrorMessage) { if (string.IsNullOrEmpty(otherProperty)) { throw new ArgumentNullException("otherProperty"); } this.otherProperty = otherProperty; } public override string FormatErrorMessage(string name) { return string.Format(ErrorMessageString, name, otherProperty); } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if (value != null) { PropertyInfo otherPropertyInfo = validationContext.ObjectInstance.GetType().GetProperty(otherProperty); if (otherPropertyInfo == null) { return new ValidationResult(string.Format("Property '{0}' is undefined.", otherProperty)); } var otherPropertyValue = otherPropertyInfo.GetValue(validationContext.ObjectInstance, null); if (otherPropertyValue != null && !string.IsNullOrEmpty(otherPropertyValue.ToString())) { if (value.Equals(otherPropertyValue)) { return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName)); } } } return ValidationResult.Success; } } 
+4
source

I would do a check in the controller.

In the controller:

 if(model.ConfirmPassword == model.OldPassword ){ ModelState.AddModelError("ConfirmPassword", "Old and new passwords cannot be the same"); } 

In view:

 @Html.ValidationMessage("ConfirmPassword") 

Hope this helps

+6
source

You can also implement class level checking, as in the description here: http://weblogs.asp.net/scottgu/archive/2010/12/10/class-level-model-validation-with-ef-code-first-and -asp-net-mvc-3.aspx

You basically implement the Validate method of an IValidatableObject and can access any properties you want.

 public class MyClass : IValidateableObject { public string NewPassword { get; set; } public string OldPassword { get; set; } public IEnumerable<ValidationResult> Validate(ValidationContext context) { if (NewPassword == OldPassword) yield return new ValidationResult("Passwords should not be the same"); } } 
+2
source

I do not think that there is already a built-in attribute that provides this functionality. A better approach would be to create your own custom attribute, as described in detail here: http://www.codeproject.com/KB/aspnet/CustomValidation.aspx

0
source

Source: https://habr.com/ru/post/1385879/


All Articles