简体   繁体   English

Parsley:验证可选字段是否等于另一个

[英]Parsley: Validating optional field is equal to another

In using the Parsley JS library , I am having trouble validating a field is equal to another when the fields are not required.在使用Parsley JS 库时,当不需要字段时,我无法验证一个字段是否等于另一个字段。

In this case, I need to confirm an updated password matches a confirmation field.在这种情况下,我需要确认更新的密码与确认字段匹配。 When the fields are required (when the user is created) I have no problem.当需要字段时(创建用户时)我没有问题。 But when the password fields are not required (when updating the user), I cannot get Parsley to validate the field when the value is empty.但是当不需要密码字段时(更新用户时),当值为空时,我无法让 Parsley 验证该字段。

According to the docs , I should be able to use the data-parsley-validate-if-empty attribute, but this does not seem to be doing anything at all.根据文档,我应该能够使用data-parsley-validate-if-empty属性,但这似乎根本没有做任何事情。

Here's what I have:这是我所拥有的:

<div class="mb-3">

    <label for="password" class="form-label">New password (leave blank to
        remain unchanged)</label>

    <input type="password" id="password" name="password"
           class="form-control"
           minlength="6"
           data-parsley-minlength-message="Password must be at least 6 characters"/>

</div>

<div class="mb-3">

    <label for="passwordConfirm" class="form-label">Confirm new
        password</label>

    <input type="password" id="passwordConfirm" name="passwordConfirm"
           class="form-control"
           minlength="6"
           data-parsley-validate-if-empty="true"
           data-parsley-equalto="#password"
           data-parsley-minlength-message="Password must be at least 6 characters"
           data-parsley-equalto-message="Value must match Password field"/>

</div>

Parsley is loading fine- the rest of the form is validated correctly. Parsley 加载正常 - 表单的 rest 已正确验证。 I just can't get Parsley to check that the passwordConfirm field matches the password field when the value of passwordConfirm is empty.passwordConfirm的值为空时,我无法让 Parsley 检查passwordConfirm字段是否与password字段匹配。

validate-if-empty has unclear semantics for builtin validators. validate-if-empty对于内置验证器的语义不明确。 See this issue .看到这个问题 In this example, what about the min-length?在这个例子中,最小长度呢?

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

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