I'm using Validation Rules to show custom messages, and INotifyDataError for the business rules.
This one part of my code:
public class ResponseValidation : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
int? response;
bool noIllegalChars = TryParseStruct<int>(value.ToString(), out response);
if (!noIllegalChars)
{
return new ValidationResult(false, "Input is not in a correct format.");
}
else
{
value = null;
return new ValidationResult(true, null);
}
}
...
}
The validation is for a Nullable datatype, I mean the value can be null or an integer, but not an incorrect input like "5b".
The question is when it produces this error (noIllegalChars = true), how can I set to the property the value null?
EDIT: The reason which I'm doing this is because, when the user left the textbox empty (the value will be "") and technically, for the property that's a null value, but it's trying to set "" to the property.
Technically - you could pass the value by reference:
Philosophically - do you want your validation rule changing the value, or just notifying that the value is bad? From a user's point of view it could be annoying if I try to input some value and the system tells me it's not valid, but doesn't give me the opportunity to correct it, only to start over.
If the code asking for validation wants to set the value to null that's your choice:
Just my 2 cents...