Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Triggering multiple validation groups with a single button?

Let's say the page TestPage.aspx has two controls. The first control is an address control that has a validation group called "AddressGroup". This group contains several validation controls which are colated in the validation summary on that control. The second control is a credit card control and has a validation group called "CreditCardGroup". It also has several validators and a summary to display the results. To add to the problem, there are some random controls on the page that also have validators which are tied to a third ValidatorSummary control.

When the user presses the "Do it all" button, I would like the page to trigger all three validation groups. The button itself can be tied to a single group or an unlabeled group. It can not be tied to multiple groups as far as I can tell.

The solution is not to extract the validation from the controls as that would deminish the value of having them in seperate controls. Thanks for your thoughts.

like image 312
Craig Avatar asked Jun 11 '09 22:06

Craig


1 Answers

Call the Validate method for each validation group individually inside the button's click handler:

bool isValidTest = false;
Validate("AddressGroup");
isValidTest = IsValid;
Validate("CreditCardGroup");
isValidTest &= IsValid;
// etc.
if (!isValidTest) return;

The next problem you may encounter is that the ValidationSummary control is linked to a single validation group. The only way that I've found to display all the error messages for multiple groups (without walking the control tree) is use multiple ValidationSummary controls.

With user controls, you may want to have its Validate method perform validation for all the controls it contains and display its own summary.

Edited to add: The isValidTest variable is not needed. According to the docs:

Note that when you call the Validate method, the IsValid property reflects the validity of all groups validated so far.

like image 96
Jamie Ide Avatar answered Nov 05 '22 10:11

Jamie Ide