What I have so far is: A WPF application, using MVVM, IDataErrorInfo implemented. Everything is working as expected.
Every time I open a “Create New Entity” dialog, the user is saluted with a nice form, flashing red all over the place. This is expected in a pure validation point of view, but it is annoying and I would like to be able to do either one of two thing.
- “Soften” the red error highlighting color to lets say orange, or yellow, to indicate “Required field”. Subsequently, when the user starts interacting with the controls switch to the red highlighting
- If that’s not possible, is there a way to disable error highlighting @ initial state?
I found this post How can I get WPF to NOT display validation errors upon initial display of control? dealing with the same problem, but it is not really answered.
I considered implementing a “Clear All error” within my IDataErrorInfo implementation, as well as implementing an InitialState flag, so errors wont be added until at least one field was changed by the user, but those have side-effects.
- If I clear all errors after init, the validation is off, but the submit button is, of course, enabled 🙁
- If I use an InitialState flag, which is also bound to the submit button to disable it… I get one step further, but then ALL errors are added with the change in one single property field.
So before I go and add InitState flags for EACH property, I though I stop and ask if there might be a simpler, more generic solution to this.
I can post some code if required. However, I implement the IDataErrorInfo in a standard way, a Dictionary<string,string> for the error messages, the Property Fields with OnChange Events and a switch case statement to call individual validation methods.
EDIT:
Here is what I ended up with.
- I added an
IsInitState=trueflag for each ‘Required’ field property in the VM -
In the setter code this flag is set to false on first change
if (Name != value) { Name = value; IsInitState_Name = false; base.OnPropertyChanged("Name"); } -
In the ValidateName (), the Null/empty test is conditional based on the flag
if (!IsInitState_Name && String.IsNullOrEmpty(this.Name))
-
Submit button “canExecute” checks if all flags are == false before enabling submit
-
It’s some extra work, but only needed for “required / non-null able” properties. Now the Create new interface is initially without error messages yet validation is fully functional
One option is to not consider the initial ’empty’ state to be invalid (thus not causing the UI to reflect an error), but to do a separate set of checks (including non-empty) before allowing a Commit. In this case, you can set an error for each property that was ’empty’ when the Commit was attempted. One consequence of this is that making a box empty by editing the text will clear any error on the box, but that may not be too terrible.