I’m working on an MVC app, and a bit of model code I’ve written has unfolded somewhat like this:
public class SomeModel
{
public int? CodeA { get; set; }
public int? CodeB { get; set; }
[RequiredIf("CodeA", 3, ErrorMessage = "(required for [Something]!)")]
[RequiredIf("CodeB", 99, ErrorMessage = "(required for [Other]!)")]
public string Foo { get; set; }
// SNIP: Unimportant details
}
Note: The RequiredIf() implementation I am using is found here.
I have decorated property Foo, which a user has the ability to edit in certain circumstances, with two RequiredIf() attributes. There are two different cases in which it is required to be filled out. In all other circumstances, the front end will parse the user’s input and populate it for them ‘behind’ the scenes.
Question: If only one case (e.g. CodeA = 3, CodeB = 4) is satisified, and the user fails to enter anything thus causing a negative validation, will the model still be marked as Invalid and a ErrorMessage logged for it? Or, since the Code B condition is satisfied, will that override the validation performed if CodeA is in a state that it is required (and not entered)?
Another way of asking: are validations additive, or is there an implicit limit to the results of only one validation at a time?
Validation is negative. For validation to pass, ALL validators must confirm the field is valid. So for your
Foo, if theCodeAvalidator passes and theCodeBvalidator fails, validation will fail. Modelstate will contain a single error for that field. If both fail, modelstate will contain two errors for that field.