bootstrapvalidator使用自定义消息手动触发

时间:2014-10-26 19:50:16

标签: jquery twitter-bootstrap twitter-bootstrap-3 jqbootstrapvalidation

我使用bootstrapvalidator并且可以正常使用以下内容。

$('.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'
                }
            }
        }
    }
});

我的提交操作向服务器提交ajax请求,例如在json中回答

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

如何在bootstrapvalidator表单输入字段上手动触发这些服务器错误?

1 个答案:

答案 0 :(得分:1)

使用remote validator,如下所示:

$('.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/'
                }
            }
        }
    }
});