programer.tips, another cool thing from @nghuuphuoc!

Integrating with Rails

Examples

Improve it on Github

For Rails, the input field is constructed from model name and field name. For example, user have email as field, when form helper render view, the input field name will be 'user[email]'. The syntax for calling the plugin looks like below:

$(form).bootstrapValidator({
    fields: {
        'user[email]': {
            validatorName: validatorOptions
        }
    }
});

When using BootstrapValidator in a Rails remote form, in order to prevent the form from multiple submissions, trigger the success.form.bv event as following:

$(form)
    .bootstrapValidator({
        ...
    })
    .on('success.form.bv', function(e) {
        // Called when the form is valid
        var $form = $(e.target);
        if ($form.data('remote')) {
            e.preventDefault();
            return false;
        }
    })
    .on('submit', function(e) {
        var $form = $(e.target);
        if ($form.data('remote')) {
            var numInvalidFields = $form.data('bootstrapValidator').getInvalidFields().length;
            if (numInvalidFields) {
                e.preventDefault();
                return false;
            }
        }
    });

Resources