How to set a default value when produces an error using validation rules?

1.2k views Asked by At

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.

3

There are 3 answers

0
D Stanley On BEST ANSWER

Technically - you could pass the value by reference:

public override ValidationResult Validate(ref object value, System.Globalization.CultureInfo cultureInfo)

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:

int? result;
string input = "5b"

ValidationResult response = ResponseValidation().Validate(input, CultureInfo.CurrentCulture);
if (!response.IsValid) 
    result = null;
else
    result = int.Parse(input, CultureInfo.CurrentCulture);

Just my 2 cents...

0
Steven P On

You can't unless value is a ref param. If you're trying to separate logic though, the decision as to whether or not the data is valid should be different from what you want to do AFTER that decision, such as clearing the input field.

For example:

if (!rule.Validate(val, CultureInfo.CurrentCulture).IsValid)
{
    //Clear the field... whatever else you want to do
}
0
slugster On

The previous answers have mentioned passing the value into the Validate function byref - not only is this is semantically incorrect as they mention, but if you create an overloaded Validate function that does this then the binding subsystem will not call it if you declare the validation rule within the XAML. Sure it can be called programmatically, but you are shooting yourself in the foot if you start creating ValidationRules with code that can only be reached in that way.

A more appropriate action to take is to use a converter on the field that returns a default int? if an empty string is passed in.