fifamaniac04
fifamaniac04

Reputation: 2383

Removing validations from bindings

I have a textbox whose text I want to validate only sometimes based on a check box value. when it's check, use validation, when not checked don't validate.

I can get it to validate fine and the red box appears around it but then when i don't want to validate it, the red box around the textbox stays. I have tried clearing the bindings pon the text box but no luck.

Upvotes: 1

Views: 90

Answers (1)

Saber Amani
Saber Amani

Reputation: 6489

In your case you need to implement custom validation something like this :

public class TestModel : ValidationRule
{
    public bool IsChecked { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        if (IsChecked)
        {
            if (string.IsNullOrEmpty(FirstName))
            {
                return new ValidationResult(false, "FirstName requierd.");
            }
        }
        return new ValidationResult(true, null);
    }
}

Hope this help.

Upvotes: 1

Related Questions