Enable submit button in Bootstrap validator

Umerm picture Umerm · Nov 4, 2014 · Viewed 10.1k times · Source

I am using Bootstrap validator and the problem I am having is that I want to enable to submit button after all values are valid but unable to do so.

$(document).ready(function() {
    $('#ans_frm').bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        submitButtons: 'button[type="submit"]',
        fields: {
            ans: {
                group: '.col-md-8',
                validators: {

                    stringLength: {
                        min: 3,
                        max: 100,
                        message: 'The answer must be more than 2 and less than 100 characters long'
                    },
                    notEmpty: {
                        message: 'The answer must not be empty'
                    }
                }

            }
        }
    }).on('status.field.bv', function(e, data) {

                disableSubmitButtons(false);
            }
        });
});

Answer

dannytranlx picture dannytranlx · Nov 5, 2014

You need to disable the submit button .on('error.field.bv') and enable it back on .on('status.field.bv').

And you should be using data.bv.disableSubmitButtons() method!

Can you try this?

$(document).ready(function() {
    $('#ans_frm').bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        submitButtons: 'button[type="submit"]',
        fields: {
            ans: {
                group: '.col-md-8',
                validators: {
                    stringLength: {
                        min: 3,
                        max: 100,
                        message: 'The answer must be more than 2 and less than 100 characters long'
                    },
                    notEmpty: {
                        message: 'The answer must not be empty'
                    }
                }
            }
        }
    }).on('error.field.bv', function(e, data) {
            data.bv.disableSubmitButtons(true); // disable submit buttons on errors
        }
    }).on('status.field.bv', function(e, data) {
            data.bv.disableSubmitButtons(false); // enable submit buttons on valid
        }
    });
});