我编辑表单时引导验证器问题

时间:2014-11-10 10:23:05

标签: validation twitter-bootstrap-3

我有一个表单,我使用了字段验证器并且工作正常但是当我在编辑模式下打开表单并且空字段并按下save bootstrap验证器不起作用。

以下是验证码

$(document).ready(function () {
    $('#frmSpecialtyDetail')
        .bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                ShortName: {
                    group: '.col-md-6',
                    validators: {
                        notEmpty: {
                            message: 'Short Name is required'
                        }
                    }
                }
            }
        });
});

1 个答案:

答案 0 :(得分:0)

从最初的BootstrapValidator网站获取,这是您必须使用的代码,以便在表单验证成功后提交表单:

$(document).ready(function() {
  $(form)
    .bootstrapValidator({
        ... options ...
    })
    .on('success.form.bv', function(e) {
        // Prevent form submission
        e.preventDefault();

        // Get the form instance
        var $form = $(e.target);

        // Get the BootstrapValidator instance
        var bv = $form.data('bootstrapValidator');

        // Use Ajax to submit form data
        $.post($form.attr('action'), $form.serialize(), function(result) {
            // ... Process the result ...
        }, 'json');
    });
});

在你的情况下,我会按照以下方式做点什么:

$(document).ready(function() {
  $('#frmSpecialtyDetail')
    .bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            ShortName: {
                group: '.col-md-6',
                validators: {
                    notEmpty: {
                        message: 'Short Name is required'
                    }
                }
            }
        }
    })
    .on('success.form.bv', function(e) {
        // Prevent form submission
        e.preventDefault();

        // Get the form instance
        var $form = $(e.target);

        // Get the BootstrapValidator instance
        var bv = $form.data('bootstrapValidator');

        // Use Ajax to submit form data
        $.post($form.attr('action'), $form.serialize(), function(result) {
            // ... Process the result ...
        }, 'json');
    });
 });

希望有所帮助。

来源:http://bootstrapvalidator.com/examples/ajax-submit/