Elegant way to make CustomValidator work with ValidationSummary messagebox

后端 未结 7 2047
耶瑟儿~
耶瑟儿~ 2020-12-14 08:40

I have run into this problem before but never quite solved it. I have a form with several validators and also a CustomValidator.



        
相关标签:
7条回答
  • 2020-12-14 09:17

    bduke's RegisterServerValidationMessageScript is "faking it", but it isn't. It really fixes the problem. Every Utility namespace needs this function somewhere.

    0 讨论(0)
  • 2020-12-14 09:20

    I have found a workaround for when javascript is disabled and the ValidationSummary does not show the errorMessage property of the CustomValidator. This is required as injecting script or alerts, as described above, will not work.

    Add a new Validator control, let's call it CustomValidatorProxy, set its ControlToValidate property to any one of the controls on the form and EnableClientScript=false.

    Within the ServerValidate event handler perform your custom validation and if validation fails set the IsValid property of the CustomValidator and CustomValidatorProxy to false and similarly set both ErrorMessage properties.

    If the validation in the ServerValidate is passed ensure that the IsValid property of the CustomValidatorProxy is set to true.

    Provided the CustomValidatorProxy is before the CustomValidator in the ValidatorCollection of the Page then the ServerValidate handler will override the IsValid property value that the CustomValidatorProxy would have returned having validated the ControlToValidate value and your ErrorMessage from your CustomValidator will be displayed in the ValidationSummary.

    0 讨论(0)
  • 2020-12-14 09:26

    You should write a property

    ValidationGroup="ValidationSummary1"

    at every validator in your case.

    Also check if your page has

    AutoEventWireup="true"
    
    0 讨论(0)
  • 2020-12-14 09:27

    The ShowMessageBox option is fully client-side, so it will only evaluate if you have set the ClientValidationFunction on the CustomValidator.

    You can also fake it by registering a script that makes an alert, so when you get back from the server's validation, it'll prompt with the error message. This can either be registered in the ServerValidate method (per @Jose Basilio), or you can call the following method during the PreRender event to register a popup with all invalid validators on the page:

        /// <summary>
        /// Registers a script to display error messages from server-side validation as the specified <see cref="UserControl"/> or <see cref="Page"/> loads from a postback.
        /// </summary>
        /// <remarks>
        /// Must be called in the PreRender if used to validate against the Text property of DNNTextEditor controls, otherwise Text will not be populated.
        /// Must set the ErrorMessage manually if using a resourcekey, otherwise the resourcekey will not have overridden the ErrorMessage property.
        /// </remarks>
        /// <param name="ctrl">The <see cref="UserControl"/> or <see cref="Page"/> which is being posted back.</param>
        /// <param name="validationGroup">The validation group against which to validate.</param>
        public static void RegisterServerValidationMessageScript(TemplateControl ctrl, string validationGroup)
        {
            if (ctrl != null && ctrl.Page.IsPostBack)
            {
                ctrl.Page.Validate(validationGroup);
                if (!ctrl.Page.IsValid)
                {
                    StringBuilder errorMessage = new StringBuilder("<script language='javascript'>alert('");
                    for (int i = 0; i < ctrl.Page.Validators.Count; i++)
                    {
                        IValidator validator = ctrl.Page.Validators[i];
                        if (!validator.IsValid)
                        {
                            errorMessage.Append("- " + validator.ErrorMessage);
                            if (i < ctrl.Page.Validators.Count - 1)
                            {
                                errorMessage.Append(@"\r\n");
                            }
                        }
                    }
    
                    errorMessage.Append("');</script>");
                    ctrl.Page.ClientScript.RegisterStartupScript(typeof(IValidator), "validationAlert", errorMessage.ToString(), false);
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-14 09:28

    Try using a ValidationGroup property across all your validators and the ValidationSummary.

    EDIT: Another possibility could be the Server Validation Code

    args.IsValid = (!CampaignRegistration.IsMemberRegistered(args.Value));
    

    if CampaignRegistration.IsMemberRegistered(args.Value) is returning false, "!" is making it true and therefore making it valid. I think you should get rid of the "!" as follows:

    args.IsValid = CampaignRegistration.IsMemberRegistered(args.Value);
    

    UPDATE: In order for the ValidationSummary to display your custom validator message in a messagebox, you need to have ClientValidationFunction Code. If you need to display just the summary without a popup, this is not needed.

    <asp:CustomValidator ID="cvMemberNum" runat="server" 
        CssClass="ValidationMessage" Display="Dynamic"
        ControlToValidate="txtMemberNum" ValidateEmptyText="false"
        OnServerValidate="cvMemberNum_Validate"
        ClientValidationFunction = "ClientValidate"  
        ErrorMessage="This membership number is already registered">*</asp:CustomValidator>
       //JavaScript Code.
       function ClientValidate(source, args)
       {         
          args.IsValid = false; //you need to add validation logic here
       }
    

    MORE: If you don't want to do ClientSide Validation, try this trick to show the alert. Make this change to your CustomValidator ServerValidate method:

    protected void cvMemberNum_Validate(object source, ServerValidateEventArgs args)
    {
        bool isValid = true;
        try
        {
            isValid  = (!CampaignRegistration.IsMemberRegistered(args.Value));
        }
        catch
        {
            isValid = false;
        }
        args.IsValid = isValid;
    
        if(!isValid)
        {
           if(!Page.IsClientScriptBlockRegistered("CustomValidation")) 
             Page.RegisterClientScriptBlock("CustomValidation", "<script>alert('This membership number is already registered');</script>"); 
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-14 09:31

    I've recently had same problem. ValidationSummary was not showing the ErrorMessage from CustomValidator when ServerValidate stated validation failure. Since by default (as my little reverse engineering showed) validation summary is rendered client side on postback I've simply added a script that checks all validators on document load/async postback completion and triggers validation summary creation for failed validation groups:

    $(document).ready(function () {
        var displayAlert = function () {
            if (typeof Page_Validators == 'undefined') return;
    
            var groups = [];
            for (i = 0; i < Page_Validators.length; i++)
                if (!Page_Validators[i].isvalid) {
                    if (!groups[Page_Validators[i].validationGroup]) {
                        ValidationSummaryOnSubmit(Page_Validators[i].validationGroup);
                        groups[Page_Validators[i].validationGroup] = true;
                    }
                }
        };
    
        displayAlert();
    
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(
                        function () {
                            displayAlert();
                        });
    }
    );
    

    In my scenario I had nested user controls with validators, update panel and validation summary at the parent page.

    More details here.

    0 讨论(0)
提交回复
热议问题