I’m writing a view model which derives from ReactiveValidatedObject. This allows me to use Data Annotations to trigger validation on the view, but I’ve run into a problem: the validation rules are evaluated in what seems to be a random order.
For example, if the property is declared like this (the model’s field is a decimal, and the method validator just does decimal.TryParse and checks to see if the value is >= 0):
private string abc;
[ValidatesViaMethod(ErrorMessage = "Invalid", AllowNull = true]
[Required(ErrorMessage = "Required")]
public string Abc { /* ... */ }
I have unit tests that make sure the behavior is as follows:
- If the field is blank, the error should be “Required”.
- If the field has something that doesn’t parse, the error should be “Invalid”.
Occasionally, I’ll change something in another file, run all the tests, and this one will fail. Swapping the order of the attributes in the property declaration fixes the test.
Is there any way to specify in which order these attributes are applied?
I don’t believe there is any way to specify the order, it’s the order in which Reflection returns them. You might have to rig your IsAbcValid to detect null and (incorrectly) mark it as Valid.