While the IgnoreNulls attribute can be added, it leads to convoluted ValidationResults when validation results are returned. This is because the validation block implicitly wraps the validators into an OrCompositeValidator - the property can be null OR it can be an integer in the range specified.
When the validation fails, the top-level validation result is for the OrCompositeValidator. To get the actual RangeValidator validation result, you now need to drill down into the NestedValidationResults property on the ValidationResult object.
This seems like a lot of work to process validation result messages, so it seemed to me that there had to be a better way.
Here is what I did.
- I created a class called IgnoreNullStringLengthValidator that inherits from the StringLengthValidator (here you would inherit the RangeValidator).
- Create all the constructors needed to support the base constructors.
- Override the DoValidate method and check for a null value - here you would write: if (!objectToValidate.HasValue) return;
- Make sure your next line of code calls base.DoValidate(...).
- Created an attribute class called IgnoreNullStringLengthValidatorAttribute that returns the new IgnoreNullStringLengthValidator. Here, you would create an IgnoreNullRangeValidatorAttribute class.
The resulting validation result is much more in line with what you'd expect because it does not nest your validators implicitly.