Search code examples
javascriptjquerytwitter-bootstrapvalidationbootstrapvalidator

How do I reset the BootstrapValidator without deleting data from the form?


I am using BootstrapValidator in a project. After the validator has been initialized $(formSelector).bootstrapValidator(/*plugin options*/), other javascript may enable or disable some input fields. I need to restart the validator without clearing the input fields, so that the validator is checking all enabled fields and ignoring the disabled ones.

I have tried $(formSelector).bootstrapValidator('resetForm', true); after disabling/enabling fields, but this clears the data from the input fields.

If I go without resetting, the newly-enabled fields are not validated, and the disabled fields prevent submittal.

How can I restart the BootstrapValidator?

Edit: I tried the solution in the other question. It did not solve the problem, presumably because the Validator wasn't acting on those fields in the first place, so it couldn't be 'reset'?


Solution

  • I think I've solved this, by first destroying the BootstrapValidator instance, and then re-initializing it.

    We have a function for the initialization,

    function initFormValidation () {
        $(formSelector).bootstrapValidator(/*plugin options*/)
    }
    

    after enabling/disabling input fields, we destroy the instance

    var bootstrapValidator = $(form).data('bootstrapValidator');
    
    if (bootstrapValidator != undefined)
        boostrapValidator.destroy();
    

    and re-initialize it

    initFormValidation();