Is it possible to have unobtrusive validation to make a field required but only if other properties change?
For Example
[Required]
public Decimal Income {get; set;}
[Required]
public Decimal Tax {get; set;}
//Required if tax or income changes
public string ChangeReason {get; set;}
I thought about having multiple backing store fields and writing a Custom Validator to compare these, but wondered if anyone had a better suggestion?
Custom Validator is the way to go. I had to build something similar a while back.
I’d set up a hidden value – “Changed” – set it to true whenever the user mods the fields of interest.
Set a RequiredIf validator on the 2 properties of interest:
The code for a RequiredIf validator is shown below:
Javascript: