bootstrapValidator: How do you add and remove validators dynamically to an existing input field?

倾然丶 夕夏残阳落幕 提交于 2019-12-06 10:47:56

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.

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();

update

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

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

(thanks to @nghuuphuoc for pointing this out)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!