
Validation Groups
Imagine a page with a group of fields for collecting data and separate group of
fields for searching your site. Each group of fields has its own Submit button.
When the user clicks one button, the validators of the other group should not
be validated and block submitting the page. With the Validation Groups feature,
you can assign a group name to the Group property of each
validator, the ValidationSummary and the submit buttons.
Microsoft's validators in ASP.NET 2.0 implement a very similar system. However,
Peter's Professional Validators has several improvements over the native
validators:
-
The Submit button, Validators, and ValidationSummary controls can be in more
than one validation group. For example, you want one ValidationSummary control
on the page to handle all of your ValidationGroups. Another example, you have a
textbox that is used in two different validation groups to which you want one
validator assigned that covers both groups.
-
Some people assemble a DataGrid or Repeater with textboxes on every row. They
want the user to be able to add data into any of those rows but when they click
submit on the row, it shouldn't validate any other row. This forces you to have
a unique Group name for each row. DES lets you put the "+" character in front
of the group name and it creates a unique group name for the row.
This demo page has two groups of fields. Each has its own Submit button that
will only validate its own field. Click each Submit button to see its own
validation error appear. (Leave the textboxes blank as its validating the
textbox has text.) Use the Reset button to start over.
Group 1
Group 2
ASP.NET Syntax for this demo
<div width="100%" runat="server" id="Group1" style="BORDER-RIGHT: coral
thin solid; BORDER-TOP: coral thin solid; BORDER-LEFT: coral thin solid;
BORDER-BOTTOM: coral thin solid; BACKGROUND-COLOR: antiquewhite">
<b>Group 1</b><br>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<des:RequiredTextValidator id="RequiredTextValidator1" runat="server"
ControlIDToEvaluate="TextBox1" ErrorMessage="This field is
required" Group="Group1">
<ErrorFormatterContainer>
<des:TextErrorFormatter
Display="Dynamic"></des:TextErrorFormatter>
</ErrorFormatterContainer>
</des:RequiredTextValidator>
<des:DataTypeCheckValidator id="DataTypeCheckValidator1" runat="server"
ControlIDToEvaluate="TextBox1" ErrorMessage="Enter a number"
DataType="Integer" Group="Group1">
<ErrorFormatterContainer>
<des:TextErrorFormatter
Display="Dynamic"></des:TextErrorFormatter>
</ErrorFormatterContainer>
</des:DataTypeCheckValidator>
<br>
<des:Button id="Button1" runat="server" Group="Group1" Text="Submit"></des:Button><br>
</div>
<br><br>
<div width="100%" runat="server" id="Div1" style="BORDER-RIGHT: royalblue
thin solid; BORDER-TOP: royalblue thin solid; BORDER-LEFT: royalblue thin
solid; BORDER-BOTTOM: royalblue thin solid; BACKGROUND-COLOR: lightblue">
<b>Group 2</b><br>
<asp:TextBox id="Textbox2" runat="server"></asp:TextBox>
<des:RequiredTextValidator id="Requiredtextvalidator2" runat="server"
ControlIDToEvaluate="TextBox2" ErrorMessage="This field is
required" Group="Group2">
<ErrorFormatterContainer>
<des:TextErrorFormatter
Display="Dynamic"></des:TextErrorFormatter>
</ErrorFormatterContainer>
</des:RequiredTextValidator>
<des:DataTypeCheckValidator id="DataTypeCheckValidator2" runat="server"
ControlIDToEvaluate="TextBox2" ErrorMessage="Enter a number"
DataType="Integer" Group="Group2">
<ErrorFormatterContainer>
<des:TextErrorFormatter
Display="Dynamic"></des:TextErrorFormatter>
</ErrorFormatterContainer>
</des:DataTypeCheckValidator>
<br>
<des:Button id="Button2" runat="server" Group="Group2" Text="Submit"></des:Button><br>
</div>
<des:Button id="Button3" runat="server" Group="*" Text="Validation
Both Groups"></des:Button>
<input type="reset" value="Reset">
|