How To -Disable validation on Disabled controls

Topics: Validation Application Block
Dec 29, 2009 at 2:03 AM

I need to enable/disable the validation of controls based on scenarios, so I have to do it in code, but I could not see the related properties in run time which I could see in design time.

Kindly help.

Thanks.

Dec 29, 2009 at 4:55 AM

Are you working on winforms or web app?  The ValidationProvider you use on winforms and the PropertyProxyValidator for the web form both has the Enabled property which you can configure.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Dec 29, 2009 at 5:04 AM

Hi,

I am using Winforms and Entlib version 3.1. I do not want to disable the validation completely.

I need to enable or disable validation on specific fields during runtime. How can I do that ?

Thanks.

Dec 29, 2009 at 5:25 AM

I see.  Those properties are not available as they are properties of a ValidatedControlItem object.  The workaround I can see is to have a logic of checking if the control needs to be validated during the Validating event of the control and other events which will trigger the validation of the control.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Dec 29, 2009 at 7:34 AM

Hi Sarah,

I had set the properties required for the validation in design time.

I tried this piece of code and assigned to the validation event for all the controls that needs to be validated.

        private void EnableDisableValidation(object sender, CancelEventArgs e)
        {
            if (((Control)sender).Enabled == false)
                EventValidationProvider.SetPerformValidation((Control)sender, false);
            else
                EventValidationProvider.SetPerformValidation((Control)sender, true);
        }

But I get an error when I call this.ValidateChildren() method. Error details as below

Message: {"Object reference not set to an instance of an object."}

Source: "Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WinForms"

Name: "GetExistingValidatedControlItem"

Any idea whatz wrong ?

Thanks.

Jan 5, 2010 at 3:04 AM

I don't have any idea yet.  When would you call the this.ValidateChildren() so I could repro it?

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Jan 6, 2010 at 8:06 AM

Hi Sarah,

I have  different modes in this particular windows form. Controls, will be enabled/disabled based on the mode. I don't want to validate the disabled controls.

 this.ValidateChildren() - will be called on the click of a button. When any control is disabled I get the above error.

Any idea ?

Thanks

 

Jan 6, 2010 at 8:46 AM

I still can't repro your error, could you repro it in o sample project?

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com