vuelidate:验证依赖于其值的项目列表

Mas*_*ske 5 vue.js vuelidate

我需要验证与元素本身的值相关的列表元素。

是否有可能或者我应该为每个产品创建一个验证?

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?
        }
      }
    }
  }
})
Run Code Online (Sandbox Code Playgroud)

https://jsfiddle.net/e0tL4yph/

Mas*_*ske -3

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

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

amount: {
  ltePending: (amount, { pending }) => amount <= pending
}
Run Code Online (Sandbox Code Playgroud)

它按我想要的方式工作!