Search code examples
asp.nettwitter-bootstrapvalidationupdatepanelformvalidation-plugin

FormValidation.io won't work within UpdatePanel?


I'm using the formvalidation.io plugin to perform client-side validation on my pages. I've recently come across a problem where I cannot get the validation to work after a postback within an UpdatePanel (the validated controls are within the UpdatePanel as well).

Start of UP:

<asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional">
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="ActivityType" EventName="SelectedIndexChanged" />
                        <asp:AsyncPostBackTrigger ControlID="ActivitySubType" EventName="SelectedIndexChanged" />
                    </Triggers> .... </asp:UpdatePanel>

The validation works as intended until a postback occurs. Each time ActivityType or ActivitySubType perform a postback, all validation disappears.

Javascript / jQuery:

 $(function () {
        docReady(); 
    });

    var prm = Sys.WebForms.PageRequestManager.getInstance();

    prm.add_endRequest(function () { docReady(); });

    function docReady() {
        $("#aspnetForm").formValidation({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {

                ctl00$maincontent$ActivityDate:
                        {
                            validators: {
                                notEmpty: {
                                    message: 'Date / Time is required'
                                }
                            }
                        },
                ...
            }
        }); } });

I've tried doing the validation declaratively as well with the same result. I've also tried re-initializing the validation using the console after postback with no luck.

I'm a bit frustrated b/c I've tried everything I know to do. Thanks!


Solution

  • I have the same problem. As you've seen the validation can work initially, but after a postback, it stops working. I tried using the javascript pageLoad() function which gets called by the asp framework after a postback is completed.

    I have found that setting the formValidation to null each after each postback seems to resolve the problem. See my code below. I call the isFormValid() function from my asp submit button.

    <script type="text/javascript">
    
        function isFormValid() {
            var formValidation = $('form').data('formValidation');
            formValidation.validate();
            if (formValidation.isValid() != true) {
                    return false;
            }
            return true;
        }
        // called by Ajax Framework after postback
        function pageLoad() {
            setupValidation();
        }
    
        function setupValidation() {
             $('form').data('formValidation',null);
             $('form').formValidation({
                framework: 'bootstrap',
                icon: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                }
            })
        });
    </script>
    

    In your case - try the following:

    $("#aspnetForm").data('formValidation',null);
    $("#aspnetForm").formValidation({....