Print Page | Close Window

Instance Validation and UI Messages

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2753
Printed Date: 21-Apr-2025 at 2:24pm


Topic: Instance Validation and UI Messages
Posted By: LawyerDev
Subject: Instance Validation and UI Messages
Date Posted: 10-Jun-2011 at 10:00am
Greetings,

I have developed a WPF MVVM DevForce application with DevForce version 6.1.

On my main screen, I have many fields where the user enters information.  The input fields, textboxes, are binding to DevForce entity properties, which then get saved off to the database.  Before I do a save, I do instance validation on the entity.  Our business rules are very complex, so I added customer verifiers (often to check for an entered value) for most of the properties in the entity.  This is working great.  If I leave out a required value, the instance validation catches the violation as it should.

My issue is with the controls displaying the validation error.  The user is entering new information, and so we are creating a new entity and all values are empty.  When the user clicks to save the information, and the instance validation is performed, I display a message containing information about each Verifier that failed.  However, the errors are not bubbling up to the GUI and the controls. I would like the controls to also display the validation error so the user can see which fields need information.  I can get a control to display an error if I enter a value in a required field, click another control, then go back and remove the value.  However, if I skip required field and hit save, I then want the control to display the error.

Our default error notification mode is Notify.  The controls have ValidatesOnDataErrors=true and NotifyOnValidationError=true, which works perfectly if they input a value, and then later delete it as I described above.  However, it would make sense and would help the user if this also happened when they clicked save and there were required fields with missing data.

Here is an example of our binding:
<TextBox Name="txtHomePhone2" Width="120" Text="{Binding Path=DebtorPhone2,
ValidatesOnDataErrors=trueNotifyOnValidationError=true}" />

And here is an example of a verifier:
private static VerifierResult Phone2RequiredCondition(**** pTarget, TriggerContext pTriggerContext,
                                                                 VerifierContext pVerifierContext)
      {
          if (pTriggerContext != null && pTriggerContext.Timing == TriggerTiming.BeforeSet)
          {
              throw new VerifierException("Phone2Required verifier not implemented for Preset");
          }
 
          bool isOk = pTarget.RefusedInfo || (!pTarget.RefusedInfo 
                                                      && !String.IsNullOrWhiteSpace(pTarget.Phone2));
          return new VerifierResult(isOk); 
}

Any help would be appreciated.  Thanks.



Replies:
Posted By: DenisK
Date Posted: 13-Jun-2011 at 1:11pm
Hi LawyerDev;

There are actually 2 ways to approach this. 

One is to build your own ErrorProvider. Fortunately, here's a link that already provides you with an implementation to get you started.  http://www.codeproject.com/KB/WPF/wpfvalidation.aspx#errorProviderCreate - http://www.codeproject.com/KB/WPF/wpfvalidation.aspx#errorProviderCreate

Another way, which, in my opinion, is a cleaner implementation, is to disable the Save button until all validations have passed. You can do this by using CommandBinding.  http://stackoverflow.com/questions/104520/wpf-validation-for-the-whole-form - http://stackoverflow.com/questions/104520/wpf-validation-for-the-whole-form

Hope this helps.



Print Page | Close Window