ValidationAttribute based on the value of another property

28 Views Asked by At

I know how to create a ValidationAttribute. But how can I check something based on the value of another property?

For example this model :

public class MyModel
{
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get;set; }
    [Required]
    public string Country { get; set; }
    [CustomValidator(ErrorMessage =  "If BE city cannot be null...")]
    public string City { get; set; }
}

Country is mandatory but if the Country == "BE" the property City cannot be null.

Is this possible with ValidationAttribute?

0

There are 0 best solutions below