Textbox Validation works only after text change

573 views Asked by At

I have applied MVVM in my app to implement validation rules for some pages by following this tutorial and this sample so validations are working properly and it is very creative way but I have one issue while validating Textbox text in the following scenario:

After implementing Required OR Regular Expression or any other data validation rule, it won't work unless some changes happen to the Textbox text(i.e user enter some text and remove it, will trigger the REQUIRED validation and validation error will be there)

That means all TextBoxes in the page will be valid even if they are annotated by Required and they don't include any text (in case there are no changes happened to their text yet)

1

There are 1 answers

0
Sunteen Wu On BEST ANSWER

That means all TextBoxes in the page will be valid

According to the sample testing on my side, unless you invoke the following ValidateProperties() method which is for validate all the properties otherwise you will not got all validated.

  public bool ValidateProperties()
  {
      var propertiesWithChangedErrors = new List<string>();

      // Get all the properties decorated with the ValidationAttribute attribute.
      var propertiesToValidate = _entityToValidate.GetType()
                                                  .GetRuntimeProperties()
                                                  .Where(c => c.GetCustomAttributes(typeof(ValidationAttribute)).Any());

      ...
  }

ViewModel inherit from ValidatableBindableBase class which will get an Errors property. Every TextBox control bind its own Errors property. The error for one TextBox will update once this one property changed, which will not influence other TextBox since their properties don't change.

Text updated lead the first TextBox validated but not the second one:

enter image description here

Validated all:

enter image description here