bootstrapValidator:如何动态地向现有输入字段添加和删除验证器? [英] bootstrapValidator: How do you add and remove validators dynamically to an existing input field?

查看:659
本文介绍了bootstrapValidator:如何动态地向现有输入字段添加和删除验证器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态表格,该表格与knockout.js绑定并由bootstrapValidator验证.

I have a dynamic form that is bound with knockout.js and validated by bootstrapValidator.

有一个输入字段需要根据另一控件的状态进行必需验证".

There is one input field that needs to be 'required-validated' dependent on the state of another control.

输入字段:

<textarea id="inputReason" name="inputReason" rows="3" 
          class="form-control col-lg-8"
          data-bind="value: Reason" />

knockout-viewmodel的相关javascript部分:

The relevant javascript part of the knockout-viewmodel:

self.SelectAbsenceType = function (absenceType) {
    self.SelectedID(absenceType.ID);

    if (self.SelectedAbsenceType().ReasonRequired) {
        $('#formCreate').bootstrapValidator('addField', 'inputReason', {
            validators: {
                notEmpty: {
                    message: 'Please enter a reason'
                }
            }
        });
    } else {
        $('#formCreate').bootstrapValidator('removeField', 'inputReason');
    }
}

我面临的问题是对bootstrapValidator实例的removeField的调用似乎并未完全删除所有注册信息,因为bootstrapValidator类的updateStatus方法中存在一个javascript异常,实际上,根本不应该调用该函数,因为我之前已经删除了该字段:

The problem I'm facing is that a call to removeField of the bootstrapValidator instance doesnt seem to completely remove all registration infos since there is a javascript exception in the updateStatus method of the bootstrapValidator class that in fact should not be called at all since I have the removed the field before:

var that  = this,
    type  = fields.attr('type'),
    group = this.options.fields[field].group || this.options.group,
    total = ('radio' === type || 'checkbox' === type) ? 1 : fields.length;

例外:无法获取属性"group"的值:对象为null或未定义

The exception: Unable to get value of the property 'group': object is null or undefined

变量field包含值"inputReason".

The variable field contains the value 'inputReason'.

所以我的问题是这个(因为bootstrapValidators removeField的文档尚不完全清楚:如何完全删除字段inputReason的动态添加的验证?

So my Question is this (because the documentation of bootstrapValidators removeField is not entirely clear on this: How do I remove the dynamically added validation of the field inputReason completey?

(旁注:有人可以添加标记boostrapvalidator吗?)

(side note: can someone add the tag boostrapvalidator?)

推荐答案

好吧,经过一番挖掘之后,bootstrapValidator插件似乎根本不支持删除附加到输入字段的验证器,而该验证器不会在其中删除同样的过程.因此,不会取消注册附加到输入字段的触发验证的事件.

Ok, after some digging it seems that the bootstrapValidator Plugin simply doesnt yet support the removal of validators that are attached to an input field that is NOT to be removed in the same process. Thus the events that are attached to the input field that trigger the validation are not unregistered.

一个临时的解决方法是销毁bootstrapValidator实例,将表单的data-*属性设置为null并重新初始化插件.这段代码取代了bootstrapValidator.removeField()调用:

A temporary workaround is to destroy the bootstrapValidator instance, set the data-* attribute of the form to null and reinitialize the plugin. This code replaces the bootstrapValidator.removeField() call:

bootstrapValidator.destroy();

$('#formCreate').data('bootstrapValidator', null);

$('#formCreate').bootstrapValidator();

更新

另一种更好的方法是使用bootstrapValidator的启用/禁用功能:

Another even better way to get this done is to use the enable/disable feature of bootstrapValidator:

bootstrapValidator
    .enableFieldValidators
     (
        'inputReason', 
        self.SelectedAbsenceType().ReasonRequired
     );

(感谢@nghuuphuoc指出这一点)

(thanks to @nghuuphuoc for pointing this out)

这篇关于bootstrapValidator:如何动态地向现有输入字段添加和删除验证器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆