Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mvc3 validation check if property values differ

in MVC3 you can add validation to models to check if properties match like so:

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 check that two properties differ like in the following make-believe code?

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

public string ConfirmPassword { get; set; }
like image 356
adrianos Avatar asked Dec 17 '22 06:12

adrianos


1 Answers

I would do checking in the controller.

In 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

like image 197
ysrb Avatar answered Dec 30 '22 03:12

ysrb