Search code examples
vuejs3vuelidate

Vuelidate vue.js latest for vue3 using the helpers.forEach approach for array of objects


Using Vuelidate vue.js latest for vue3 using the helpers.forEach approach for array of objects. I tried running the form but the validation is causing an error given as the below

Gives $response.$errors is undefined in the console Uncaught (in promise) TypeError: $response.$errors is undefined

export default {
  setup () {
    const rules = {
      collection: {
        $each: helpers.forEach({
          name: {
            required
          }
        })
      }
    }
    const state = reactive({
      collection: [
        { name: '' },
      ]
    })
    const v = useVuelidate(rules, state)
    return { v, state }
}
}

This is the template of the code

  <div
    v-for="(input, index) in state.collection"
    :key="index"
    :class="{
        error: v$.collection.$each.$response.$errors[index].name.length,
      }"
  >
    <input v-model="input.name" type="text" />
    <div
      v-for="error in v$.collection.$each.$response.$errors[index].name"
      :key="error"
    >
      {{ error.$message }}
    </div>
  </div>
</template>

Solution

  • I need to use v$ as this was v$ represented a global variable based on useVuelidate