0

Is there a way using data annotation to compare two model properties and report error if they are the same? I know the way using Compare to report error if different but how to do it the opposite way?

For example, the code below is direct copy from the MVC RegisterModel class

        [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

In the above code, the validation will fail if the Password and ConfirmPassword are different. What I want is the opposite, I want to report the error if the Password and ConfirmPassword are the same.

I know I can create my own ValidationAttribute but just wondering if there's one already built-in for validate the opposite.

user1214916
  • 239
  • 2
  • 17
  • I don't quite understand `compare two attributes` as data annotations are attributes... did you mean `compare two properties` ? – Erik Philips May 08 '15 at 19:58
  • 1
    possible duplicate of [Using DataAnnotations to compare two model properties](http://stackoverflow.com/questions/4938078/using-dataannotations-to-compare-two-model-properties) – Erik Philips May 08 '15 at 20:00
  • Recommend you look at using [foolproof](http://foolproof.codeplex.com/) validation attributes (their `[NotEqualTo]` attribute does this –  May 08 '15 at 22:44

1 Answers1

1

For this you can use CustomValidation Decorate your Entity class with an attribute like this:

    [CustomValidation(typeof(MyEntity), "ArePropertiesEqual")]
Public class MyEntity
{
public string prop1 { get; set; }
public string prop1 { get; set; }
}

And implement a function called: ArePropertiesEqual like this:

public static ValidationResult ArePropertiesEqual(MyEntity myEntity, ValidationContext validationContext)
{
    if(myEntity.prop1 != myEntity.prop2) 
                        return new ValidationResult("propertiesdont match", new[] { "prop1", "prop2" });


    return ValidationResult.Success;
}
Aram
  • 5,537
  • 2
  • 30
  • 41
  • I want my two properties to be different so I guess my logic in the ArePropertiesNotEqual would be if (myEntity.X != myEntity.Y) return ValidationResult.Success, but what if they are same, how do I return fail? – user1214916 May 08 '15 at 21:09
  • Thanks@Aram but this turn out not what I was looking for. I guess I wasn't clear enough. I need to compare two properties in the same model class. This would only to customize that particular property validation check. – user1214916 May 08 '15 at 21:46
  • @user1214916 No, you put the custom attribute on top of your Model, and it will validate your Model class for you. as you see it compares 2 different properties of one Entity... – Aram May 08 '15 at 21:54