【问题标题】:vuelidate: validate items list that depend on its valuesvuelidate:验证依赖于其值的项目列表
【发布时间】:2017-12-04 15:50:52
【问题描述】:

我需要根据元素本身的值来验证列表中的元素。

是否可以或应该为每个产品创建验证?

new Vue({
    el: "#app",
  data: {
    text: '',
    sons: [
      {amount: 20, pending: 50}, 
      {amount: 30, pending: 150}
    ]
  },
  validations: {
    text: {
      required,
      minLength: minLength(5)
    },
    sons: {
      minLength: 3,
      $each: {
        amount: {
          maxValue: maxValue(this.sons[x].pending) // how to set x?
        }
      }
    }
  }
})

https://jsfiddle.net/e0tL4yph/

【问题讨论】:

    标签: vue.js vuelidate


    【解决方案1】:

    在 Vuelta 存储库中,我发布了这个问题,答案是:

    在这种情况下,您想使用验证函数的第二个参数。

    amount: {
      ltePending: (amount, { pending }) => amount <= pending
    }
    

    它按我的意愿工作!

    【讨论】:

      猜你喜欢
      • 2016-01-13
      • 2021-08-14
      • 2020-07-24
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      • 2014-05-05
      • 1970-01-01
      • 2021-11-16
      相关资源
      最近更新 更多