How to stop WinForm from validating on load

How to stop WinForm from validating on load

Old forum URL: forums.lhotka.net/forums/t/6605.aspx


SonOfPirate posted on Friday, March 13, 2009

I have a simple Windows Form with an ErrorProvider and BindingDataSource setup just like the examples that is used to create a new instance of my BO or to edit an existing one - same form for both purposes.

When I load the form and don't pass in the BO, it creates a new, empty BO and binds the form to it (using the BindingDataSource).  My BO has a handful of validation rules including a couple StringRequired rules.  These fields are empty by default.  As a result, when the form is loaded, I am immediately greeted with the blinking exclamation point telling me that an error exists.  I shouldn't see this until I try to do something that will cause validation to occur.

I can't figure out what is triggering validation on my BO when the form first loads.  Any ideas?

 

RockfordLhotka replied on Friday, March 13, 2009

The easiest way to do this is to do the following (general steps):

  1. Devise a property that returns true/false depending on whether you want these rules enforced (based off IsNew and anything else)
  2. Create a rule that manually short circuits based on that property value
  3. Add this stop-if-new rule at priority -1

This will prevent any "real" rules from running if the object shouldn't evaluate those rules.

Copyright (c) Marimer LLC