vuelidate icon indicating copy to clipboard operation
vuelidate copied to clipboard

Dynamic collection validation

Open aldozumaran opened this issue 6 years ago • 5 comments

i have a variable items(array of objects) with 3 attributes for every item (value, other and otherKey), for every item, the value field is required, the other field is required only if value field value is equal to otherKey field.

Look the example on jsfiddle

https://jsfiddle.net/dyquv3ek/5/

aldozumaran avatar May 13 '19 11:05 aldozumaran

Since you’re iterating over more complex structures, please create a child component that will handle those validations inside. I’d avoid using $each if possible.

shentao avatar May 13 '19 11:05 shentao

HI i am also in same kind of situation. here is my validation object

  validations: {
    filterRows: {
      $each: {
        fieldName: {
          required
        },
        operator: {
          required
        },
        value: {
          firstValue: {
            required
          },
          secondValue: {
            required // should only require if operator is equal to range.
          }
        }
      }
    }
  }

i am wondering if i can use requiredIf. I dont even know how i can use requiredIf in this case. I dont see any example of requiredIf where i can use the value of other property to see if requiredIf true or false.

manjindersarkaria avatar May 16 '19 22:05 manjindersarkaria

@sarkariajatt technically, if you pass a function to the requiredIf validator, it will give you the current value as first param, and its top level siblings as a second param. In the this context you will find your whole Vue component instance. However, this is inside an each validator, so you wont really be able to reach easily into the current looped index so to speak. As @shentao said. moving the validation into your sub component and somehow collecting the results may be your best bet for now. It is hacky. but to be honest, this will be also.

dobromir-hristov avatar May 21 '19 16:05 dobromir-hristov

Same situation here. I am rendering input fields into table, with nested JSON object data. The validation required to access variables from loop (under $iter), but just can't figure out a solution. I guess the sub-component workaround would only works for two level deep of data structure? What if it has 3 or more? Any help would be appreciated.

580 avatar Aug 14 '19 16:08 580

+1

simondoescode avatar Jun 18 '20 07:06 simondoescode