简体   繁体   English

"如何从 parsley.js 表单验证中打开\/关闭字段"

[英]How to turn on/off a field from parsley.js form validation

在此处输入图像描述 When validating an email field [name=bar] in a form #foo, I want to switch validation of that field on and off, in relation to a checkbox.在 #foo 表单中验证电子邮件字段 [name=bar] 时,我想打开和关闭与复选框相关的该字段的验证。

Reading the docs, github issues and stackoverflow answers, I thought setting data-parsley-required="false" and/or data-parsley-validate="false" would be enough.阅读文档、github 问题和 stackoverflow 答案,我认为设置 data-parsley-required="false" 和/或 data-parsley-validate="false" 就足够了。

But it turns out, that all other constraints, like email, min-lenght, max-length are still validated and the input field still validates to an error condition.但事实证明,所有其他约束,如电子邮件、最小长度、最大长度仍然有效,输入字段仍然验证错误条件。 I would prefer it to validate to success or not at all.我希望它验证成功或根本不成功。

<form id="foo">
    <input name="bar"
           maxlength="40"
           value=""
           class="form-control"
           data-parsley-validate="false"
           data-parsley-required="false"
           type="email"
           data-parsley-minlength="5"
           data-parsley-trigger="input keyup change"
           data-parsley-error-message="something is wrong">
</form>

See https://jsfiddle.net/88obg0sj/9/https://jsfiddle.net/88obg0sj/9/

So how is it possible to turn off field validation in way, it can be re-activated again?那么如何关闭字段验证方式,可以再次重新激活呢?

您应该调整excluded选项,例如,通过向其添加", [data-parsley-validate="false"]"

You can follow this way:-<\/strong>你可以按照这种方式: -<\/strong>

//destroy parsley
$('form').parsley().destroy();

//set required attribute on input to false
$('input').attr('data-parsley-required', 'false');

//reinitialize parsley
$('form').parsley();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM