3

Bootstrap Validator v 0.5.2被重新用于验证模式中的表单 (#myForm)。当表单加载到如下模式时,需要将唯一的 id(外键)动态传递给“远程”规则的“url”。

var remoteUrl = "/remoteurl/";
var id = <Foreign key of the record>

$('#myForm').bootstrapValidator({
    feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
        fieldName: {
            validators: {
                remote: {
                    url: remoteUrl + id, //dynamically passing id. // but not passing dynamically.
                    type: 'POST',
                    message: "This is the message!"
                }
            }
        } 
    }
});

问题:
在模态加载时,“id”成功地动态传递到表单中。但是,' bootstrapValidator ' 将第一个传递的 'id' 获取到表单中,除非页面重新加载。

4

1 回答 1

3

找到了解决方案!

添加隐藏输入字段以添加外键。

<input type="hidden" value="" name="foreignKey" id="foreignId">

并且,动态地将外键传递给该字段。

$('#foreignId').val(id);

然后,如下

fieldName: {
    validators: {
        remote: {
            url: remoteUrl,
             data: function(validator, $field, value) {
                return {
                    foreignKey: validator.getFieldElements('foreignKey').val()
                };
            },
            type: 'POST',
            message: "This is the message!"
        }
    }
}

现在,它对我有用。'Id' 是为远程方法动态传递的。

于 2018-05-02T12:40:35.890 回答