0

I have applied the validations of required field validation on the wpf textbox, using the ValidationRule class. My code is :

public class RequiredField : ValidationRule
{
    private String _errorMessage = String.Empty;
    public string ErrorMessage
    {
        get { return _errorMessage; }
        set { _errorMessage = value; }
    }

    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        var str = value as string;

        if (String.IsNullOrEmpty(str))
        {
            return new ValidationResult(false, this.ErrorMessage);
        }
        return new ValidationResult(true, null);
    }
}

And the XAML code is below :

<TextBox  Name="txtName"  MaxLength="50">
    <TextBox.Text>
        <Binding Path="Name" UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <myValidtaion:RequiredField ErrorMessage="Please enter Name." />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

This seems to work fine. But the issue is that after i save the valid data in database and clear the database, this validation is fired again and validation message appears again.

How can I avoid this situation ?

stukselbax
  • 5,855
  • 3
  • 32
  • 54
Tech Jay
  • 404
  • 1
  • 6
  • 16
  • Can you breakpoint in `Validate` and provide a call stack? – Millie Smith Oct 13 '13 at 06:05
  • You mean clear the field, not the database I guess? because if that is what you mean, it will treat it as a change, and hence validate it again... Kinda makes sense – Noctis Oct 13 '13 at 06:51
  • @Noctis, i understand but want to prevent this. Milie, call stack is having no big information REMTools.dll!REMTools.RequiredField.Validate(object value, System.Globalization.CultureInfo cultureInfo) Line 27 C# Am i missing anything here ? – Tech Jay Oct 15 '13 at 16:24

1 Answers1

0

This seems to be a common validation 'problem', but it is in fact the expected behviour of WPF validation... this also occurs when using the IDataErrorInfo interface.

Both of these validation methods will validate their specified properties whenever their values change, which is exactly what we want (most of the time).

I believe that the system is called predictive validation and it enables us to show the user what they have to fill in before they try to save. I personally believe that this is a better system than the old system of letting the user try to save fields with errors and then telling them afterwards that they have made an error.

Perhaps you can delay clearing the fields until the user clicks a New button, in which case the errors will only show up then?

UPDATE >>>

If you were to use the IDataErrorInfo interface, then you could add a bool IsValidating property to your data type classes that you could use to switch validation on and off. I don't have much time, so I found a post that implements this interface that I have adapted to demonstrate my point.

public class Person : IDataErrorInfo
{
    private int age;
    private bool isValidating = true;

    public int Age
    {
        get { return age; }
        set { age = value; }
    }

    public bool IsValidating 
    {
        get { return isValidating; }
        set { isValidating = value; }
    }     

    public string Error
    {
        get
        {
            return this["Age"];
        }
    }

    public string this[string name]
    {
        get
        {
            string result = null;
            if (IsValidating)
            {
                if (name == "Age")
                {
                    if (this.age < 0 || this.age > 150)
                    {
                        result = "Age must not be less than 0 or greater than 150.";
                    }
                }
            }
            return result;
        }
    }
}

Adapted from the code in the accepted answer to Creating WPF Validation error manually post

When you switch this IsValidating property to false, the property changes won't be validated:

person.IsValidating = false;
Community
  • 1
  • 1
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Hmmmm....But this will not be a good thing to delay the clearing of fields until user clicks on new button. I guess there should be some way around to get this fixed. – Tech Jay Oct 15 '13 at 16:25