Search code examples
jquerytwitter-bootstraptwitter-bootstrap-3jqbootstrapvalidation

bootstrapvalidator trigger manually with custom message


I'm using bootstrapvalidator and it works fine with the following.

$('.login-form').bootstrapValidator({
    message: 'This value is not valid',
    feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
        password: {
            validators: {
                notEmpty: {
                    message: 'The password is required and cannot be empty'
                }
            }
        },
        email: {
            validators: {
                notEmpty: {
                    message: 'The email is required and cannot be empty'
                },
                emailAddress: {
                    message: 'The input is not a valid email address'
                }
            }
        }
    }
});

My submit action submits an ajax request to the server which answers for example in json

errors: {user: 'Username does not exist', password: 'wrong password'}

How can I manually trigger these server errors on the bootstrapvalidator form input fields?


Solution

  • Use the remote validator as follows:

    $('.login-form').bootstrapValidator({
        fields: {
            username: {
                message: 'The username is not valid',
                validators: {
                    // The validator will create an Ajax request
                    // sending { username: 'its value' } to the back-end
                    remote: {
                        message: 'The username is not available',
                        url: '/path/to/backend/'
                    }
                }
            }
        }
    });