Parsleyjs:如何验证其他组输入是否成功

问题描述

我定义了一个自定义验证,用于在其他输入为空时验证某个字段是否具有值。它工作正常,但是,当用户在给定的必填字段中输入值时,我想删除其他输入的验证错误

例如,给定以下形式:

Form

对 Téléphone、Cellulaire 和 Télécopieur 输入的 focusin focusout 进行验证。如果用户点击电话,然后填写 Cellulaire 字段,电话输入应该是绿色的,而无需聚焦输入。

这是自定义验证代码

window.Parsley.addValidator('requiredWithout',{
    requirementType: 'string',validateString: function(value,inputIds,parsleyElement) {
        const inputs = inputIds.split(',').map(id => $(`#${id}`));
        
        if (value) {
            //parsleyElement.parent.validate({ group: parsleyElement.element.dataset.parsleyGroup})
            return true;
        }

        if (inputs.filter(i => i.val()).length == 0) {
            const labels = inputs.map(i => i.attr('label'));
            const messages = {
                en: 'This field is required when the following fields are empty: ' + labels.join(','),fr: 'Ce champs est requis lorsque les champs suivants sont vides: '  + labels.join(',')
            };
            return $.Deferred().reject(
                messages[window.getCurrentLocale()]
            );
        }
        return true;
    },});

如果我取消注释 parsleyElement.parent.validate({ group: parsleyElement.element.dataset.parsleyGroup}),它确实有效,但会触发错误app.js:60375 Uncaught RangeError: Maximum call stack size exceeded

我也尝试过使用事件进行验证,但最终还是出现了同样的错误

$('#customerManagementPhoneInput').parsley().on('field:validated',function() {
    customerManagementValidator.validate({group: 'phones'})
})

当必填字段之一有效时,如何消除其他元素的错误,而无需手动聚焦它们?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)