I use prism and mvvm. There is a form editor for creating and changing the entity. For fields I use validation using IDataErrorInfo.

<TextBox Text="{Binding Name, ValidatesOnDataErrors=True}" ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/> public string this[string propertyName] { get { var error = string.Empty; switch (propertyName) { case "Name": return this.ValidateNameValue(); break; } return error; } } 

The problem is that when creating a new entity, all fields have default values ​​(null / string.Empty). As a result, when opening a form for these fields, validation errors are displayed. How to achieve that validation would be started only after user input of data?

If there is at least one invalid field, then the "Submit" button is inactive. When all fields become valid, the button is activated. If the user entered valid data, and then canceled the input, the form becomes invalid and the button is disabled.

The only thing that needs to be changed is that when creating a new entity, input fields should not display the ErrorTemplate template.

  • Is the empty string the correct value? If the user closes the form with empty fields, is this normal? - VladD
  • Empty string is invalid. Initially, the form is considered invalid and the "Submit" button is inactive. - Vitaly Efimov
  • And if the user clicks on OK, what will happen? Will the unvalidated form close? Describe the desired behavior. - VladD
  • And what should happen if the user started editing the field, and then deleted all the characters? - VladD
  • Clarified the question. - Vitaly Efimov

1 answer 1

Solved this problem as follows.
In the view model made the following modifications. Added fields:

 private static readonly string[] RequiredProperties = {"Code", "Name"}; private readonly HashSet<string> invalidProperty = new HashSet<string>(); private bool isValid; 

RequiredProperties I use to update bindings, which leads to validation. invalidProperty contains property names with invalid data. If there are several validation conditions for one property, then each of them needs its own name for the invalidProperty collection. Added the following methods and properties:

 public bool IsValid { get { return this.isValid; } set { this.isValid = value; this.OnPropertyChanged(); } } public bool IsValidationEnabled { get; set; } public void CheckValidityAllProperties() { foreach (var property in RequiredProperties) { this.OnPropertyChanged(property); } } public string this[string propertyName] { get { var error = string.Empty; switch (propertyName) { //error = "Some error" } return this.IsValidationEnabled ? error : string.Empty; } } 

In the Loaded event handler for the form, I set IsValidationEnabled to true.
When you click on "Submit", a command is launched that checks IsValid. If all the data is valid, letting changes, if the data is not valid, run CheckValidityAllProperties to highlight non-valid fields. IsValid check this:

 this.IsValid = this.invalidProperty.Count == 0;