【问题标题】:Vuelidate: Make field optional based on other checkbox (should apply to custom validations as well)Vuelidate:根据其他复选框使字段可选(也应适用于自定义验证)
【发布时间】:2021-12-31 21:23:50
【问题描述】:

如何在 Vuelidate 中保持字段可选?

Vuelidate 在代码中给出$v。要检查表单验证,我们有 $invalid 道具 现在我如何根据复选框的东西保持 x 字段可选 例如。

[ ] use y field
[ y text field ] (right now optional)

但如果我选中复选框

[x] use y field
[ y text field ] (now its required and it will follow other validations such as sameAs/minLength/max/custom validation

PS:它还应该验证自定义验证。

我有这个jsfiddle 我想要的是,如果我选中 is universal 框,那么只有验证应该应用于输入框,否则验证应该被忽略,$invalid 应该是 false

【问题讨论】:

    标签: vue.js validation vuelidate


    【解决方案1】:

    您需要将validations 属性更改为函数,这将允许您使用this 访问状态。您还需要在自定义验证器中检查您的复选框值:

    validations() {
      return {
        text: {
            required: requiredIf(function () {
            return this.isUniversal
          }),
          roblox (val) {
            return this.isUniversal ? !!val.match(/roblox.com/) : true
          }
        }
      }
    },
    

    我编辑了你的jsfiddle

    【讨论】:

    • 非常感谢this 没有得到反应/正常工作。
    猜你喜欢
    • 1970-01-01
    • 2018-12-14
    • 2021-08-03
    • 1970-01-01
    • 2021-07-10
    • 2018-01-10
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多