views:

2688

answers:

3

I have a gridview that contains one to many rows (like most do) - each with an input textbox. Each row has a requiredfieldvalidator against that textbox. When the form is submitted the gridview is validated it is entirely possible that more than one row has an empty textbox. This results in repeating validation messages e.g.

  • Please provide text for 'Name' field
  • Please provide text for 'Name' field
  • Please provide text for 'Name' field

Is it possible to consolidate these messages into one message?

I know it possible to create a validator by setting up a validator class and inheriting from BaseValidator class which can be used to validate the gridview as a whole. But I put an image against each row when it is invalid so I should imagine I require separate validators on each row.

A: 

I would suggest not using a Validator Summary.

Change the Text property or inner content of the validators to something more appropriate for your application.

For example...

<asp:Validator ID="X" ... runAt="server" Text="*" />

or

<asp:Validator ID="X" ... runAt="server">*</asp:Validator>

or to display an image...

<asp:Validator ID="X" ... runAt="server"><img src="../path.png" alt="Invalid" /></asp:Validator>

I also style the validator to change to pointer to the help cursor and add a ToolTip property to show the same Error Message.

BlackMael
A: 

Thanks for the reply but unfortunately the application requires an alert box and thus the validation summary control.

Still using the validation summary control - is it possible to consolidate all messages into one message?

Peanut
this isn't a forum, respond to the answer in a comment, not another answer
mgroves
+1  A: 

This is a solution that uses a CustomValidator and requires a few organizational changes. This requires a postback since CustomValidator validation is performed on the server-side.

Here's the setup:

  1. For each of your existing RequiredFieldValidators that display the "Please provide text for 'Name' field" message you will need to set:
    • EnableClientScript="false"
    • ValidationGroup="vgTxtName" (provide your own name)
    • ErrorMessage="" (or remove it altogether; the CustomValidator will now be responsible for this)
    You have the option of displaying nothing at all (less clear to the user) or displaying an asterisk to indicate which validator is invalid.
    Option 1:
    • Display="None"
    Option 2 (preferred):
    • Display="Dynamic"
    • Set the text in between the validator tags to: *
  2. No changes needed for your ValidationSummary control (it should be neutral and not have a ValidationGroup attribute set, which is the default)
  3. Add a CustomValidator (see code below)
    • Add an eventhandler for the CustomValidator's ServerValidate event (you can just double click it from the designer to have it generated)
    • Implement the eventhandler logic (see code below)

The idea is not to directly allow the page to handle those RequiredFieldValidators anymore and instead we'll let the CustomValidator do it.

TextBox RequiredFieldValidator example (you should have something that looks like this with relevant ID names which corresponds to step 1 above):

Option 1:

<asp:RequiredFieldValidator ControlToValidate="txt1" ID="rfv1" runat="server" 
EnableClientScript="false" Display="None" ValidationGroup="vgTxtName" />

Option 2:

<asp:RequiredFieldValidator ControlToValidate="txt1" ID="rfv1" runat="server" 
EnableClientScript="false" Display="Dynamic" ValidationGroup="vgTxtName">*
</asp:RequiredFieldValidator>

CustomValidator Markup (you can place this anywhere sensible, such as next to the ValidationSummary control):

<asp:CustomValidator ID="cvName" runat="server" Display="None"
ErrorMessage="Please provide text for 'Name' field" 
OnServerValidate="cvName_ServerValidate" />

The error message here replaces the ones from the individual validators. Also notice there's no ControlToValidate set, which is valid for this type of validator and is useful for applying validation covering multiple controls.

CustomValidator EventHandler (cvName_ServerValidate):

protected void cvName_ServerValidate(object source, ServerValidateEventArgs args)
{
    // Validate vgTxtName group
    Page.Validate("vgTxtName");

    // .NET 3.5 - add using System.Linq;
    args.IsValid = Page.GetValidators("vgTxtName")
                        .OfType<RequiredFieldValidator>()
                        .All(v => v.IsValid);

    // .NET 2.0 (use either this or the above, not both)
    bool isValid = true;
    foreach (RequiredFieldValidator validator in Page.GetValidators("vgTxtName"))
    {
        isValid &= validator.IsValid;
    }
    args.IsValid = isValid;
}

That's it! Just bear in mind that this is strictly for RequiredFieldValidators. You shouldn't place different types of validators in the "vgTxtName" group since the cvName logic deals strictly with the RequiredFieldValidator type. You'll need to setup different groupings or tweak the code if you intend to use other validator types.

Ahmad Mageed