3

Hi I am working on a custom form field validator, it seems like the custom validator is working by not allowing it to continue to the next page, but it doesn't update the Validation Summary nor does it display the asterisk and the labels that i've made visable. I also have other validators like RequiredFieldValidator on the same field. My ValidationGroup is set, as is the Text and IsValid. I even wrote and set a dummy client side validation method in javascript as some workarounds suggests.

here is the validation summary code in asp.net

<asp:ValidationSummary ID="ValidatorSummary" runat="server" ValidationGroup="Step2" />

here is the custom validator and the required field one

<asp:CustomValidator ID="AddressVerification" runat="server" ErrorMessage="Please enter a valid address." Display="Dynamic" ValidationGroup="Step2" OnServerValidate="AddressVerification_ServerValidate" ClientValidationFunction="CustomValidatorDummy" Text="*" Enabled="true" EnableClientScript="true"></asp:CustomValidator>
<asp:RequiredFieldValidator ID="RFValidatorHomeAddress" runat="server" ErrorMessage="Please enter home address." Text="*" Display="Dynamic" ValidationGroup="Step2" ControlToValidate="txtHomeAddress"></asp:RequiredFieldValidator>

here is the custom validation method in the code behind

protected void AddressVerification_ServerValidate(object sender, ServerValidateEventArgs e)
{
//lets just say it doesn't validate and sets the IsValid to false
lblUspsValidatorResHomeCity.Visible = true;
lblUspsValidatorResHomeState.Visible = true;
lblUspsValidatorResHomeZip.Visible = true;
e.IsValid = false;
}

please advise, thanks.

EDIT: Answered - as bitxwise mentioned. the validation summary should be placed inside an update panel as well. Thanks!

Like so:

<asp:UpdatePanel ID="UpdatePanelValidationSummaryHome" ChildrenAsTriggers="false" UpdateMode="Conditional"
runat="server">
<ContentTemplate>
    <asp:ValidationSummary ID="AddressHomeValidationSummary" runat="server" ValidationGroup="AddressHomeValidationGroup"
        CssClass="errors" /> 
</ContentTemplate>

and then calling the update:

UpdatePanelValidationSummaryHome.Update();
David
  • 53
  • 1
  • 1
  • 6

3 Answers3

6

You seem to be missing ControlToValidate in your declaration of CustomValidator.

EDIT

If your CustomValidator aggregates multiple controls, then try this:

ASPX

<asp:TextBox ID="txtMyTextBox" runat="server" />
<asp:CustomValidator ID="AddressVerification" runat="server"
    Display="Dynamic"
    ErrorMessage="Please enter a valid address."
    OnServerValidate="AddressVerification_ServerValidate"
    Text="*"
    ValidationGroup="Step2" />
<asp:RequiredFieldValidator ID="rfvAddress" runat="server"
    ControlToValidate="txtMyTextBox"
    Display="Dynamic"
    ErrorMessage="Please enter an address"
    Text="*"
    ValidationGroup="Step2" />
...
<asp:ValidationSummary ID="ValidatorSummary" runat="server"
    ValidationGroup="Step2" />
...
<asp:Button ID="btnCheckAddresses" runat="server"
    CausesValidation="true"
    Text="Check Addresses"
    ValidationGroup="Step2" />

CS

protected void AddressVerification_ServerValidate(object source, ServerValidateEventArgs args) {
    args.IsValid = !string.IsNullOrEmpty(txtMyTextBox.Text) && !txtMyTextBox.Text.Contains(' ');
}

Note that the validation group of the control invoking the post back has CausesValidation="true" and has the same ValidationGroup as the validators.

EDIT 2

If your postback control was in the UpdatePanel but the ValidationSummary was not, then the partial postback would not have refreshed the ValidationSummary. Once you removed the postback control from the UpdatePanel, I imagine it would then generate a full postback, which would refresh your ValidationSummary.

I don't know what else is in your UpdatePanel, but many people report having issues with their validators being in UpdatePanel's.

Check out MSDN,

When you use the ValidationSummary control inside an UpdatePanel control, make sure that the validator control and the control it is associated with are in the same panel. For more information about using the UpdatePanel control for partial-page updates, see Partial-Page Rendering Overview.

as well as this MSDN blog.

bitxwise
  • 3,534
  • 2
  • 17
  • 22
  • 1
    I read that the ControlToValidate is not required for a CustomValidator. Also, my CustomValidator aggregates data from multiple TextBox Controls. Also note that this code works on a different page which is really messing with my head right now. ha, maybe I missed something. – David Nov 29 '10 at 22:15
  • That's correct. I didn't know you had more than one control to validate in your CustomValidator and have updated my answer to reflect that. I've tested the code in my post and it works for me. – bitxwise Nov 29 '10 at 22:29
  • I did that as well. The validation works, I can step into it. It's just that it doesn't update the ValidationSummary . It validates everything, just doesn't provide good UX if it doesn't update the summary about what's going wrong. The other validator does update the validation summary. I can set an update panel as a work around but there should be no reason why the summary or the labels doesn't update. ...maybe it has something to do with postback? It's driving me crazy because I know the code works, I know your code works. errrr – David Nov 29 '10 at 22:33
  • Try simplifying your code to be a bit more bare bones. Put the validation code onto its own page and ensure that it works with just your textboxes, validators, and postback control. Then start adding other elements onto the page and see what's breaking it. Did you see the note I wrote below the code? – bitxwise Nov 29 '10 at 22:41
  • I fixed it, apparently the code that was handed down to me had the Button control inside an Update Panel, I saw that there was no reason why it should be inside an update panel, and it wasn't being used or called in the code behind or anywhere else so I removed it. Voila, it works! Now another question arises, why does having a Button inside a Update Panel break everything. – David Nov 29 '10 at 23:10
  • Is the ValidationSummary in the UpdatePanel as well? – bitxwise Nov 29 '10 at 23:13
  • it was just the button that was in the update panel... yeah, I'll have to look into it further. weird. thanks for the help :) – David Nov 30 '10 at 00:58
  • If you check out my answer - EDIT 2 - you'll see why your ValidationSummary didn't update. The postback resulting from your button was partial to the UpdatePanel that contained it. Since the ValidationSummary was not in the UpdatePanel, it wouldn't refresh. – bitxwise Nov 30 '10 at 01:01
  • that makes sense, but why did RequiredFieldValidator update the ValidationSummary? – David Nov 30 '10 at 14:51
  • RequiredFieldValidator does not require a postback to update ValidationSummary. It's triggered prior to the postback and actually prevents postback if the validator deems the form invalid through translated javascript. Keep in mind that your CustomValidator is implementing a server validation rather than a client one. – bitxwise Nov 30 '10 at 14:55
0

Make sure every control (textbox, checkbox, etc) that is being validated, every RequiredValidator, CustomValidator and ValidationSummary has the same ValidationGroup value.

ie.

<asp:CustomValidator ID="CustomValidator6" runat="server" ErrorMessage="The field is required"
ValidationGroup="myValGroup">*</asp:CustomValidator>

Of course this will only work if all the controls are inside the same panel or parent control.

LoftyWofty
  • 87
  • 1
  • 8
0

In my case the validation summary was not showing because the submit button was in a separate update panel.

<Triggers>                        
<asp:PostBackTrigger ControlID="ButtonSubmit" />
</Triggers>

Once I added the above code the summary appeared.

StevenC
  • 75
  • 1
  • 4