Title: Conditional Validation for ViewModel Fields on ASP MVC
I have a question about ASP MVC validtion. Let's say that I have the follwing View Model
public class PersonViewModel
{
[Required]
public string Name {get; set; }
[Required]
public string Email {get; set; }
}
According to this when I submit the form MVC will validate that both fields have values. However, in my website I have the situation where the Email can be turned off in a global site setting, so the model will only render the Name Textbox on the form. Now when I submit the form it still asks me for the Email field since it is indicated as "Required", despite there is no way the user can fill that field now.
Is there a solution for this type of scenario when using ASP MVC validations?