question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

forEach helper makes new entries to the collection dirty

See original GitHub issue

Using the sample code given in the docs for forEach:

<template>
  <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>
<script>
// setup in a component
import { helpers, required } from '@vuelidate/validators'
import useVuelidate from '@vuelidate/core'
import { reactive } from 'vue'

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

</script>

If I call $touch on collection, and then a new element is added to the collection, it’ll be dirty as soon as it’s added. I would expect that the existing elements would be dirty, and the new ones wouldn’t. That way, errors wouldn’t be displayed immediately and would instead require the user to interact with the element, as it’s intended.

For example, this sample from an older version of vuelidate does just that: https://vuelidate.js.org/#sub-collections-validation

How do I get this behavior in the current version?

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dobromir-hristovcommented, Jun 22, 2022

Don’t use the helper, use child components. Sorry that helper was just to help with very simple scenarios

0reactions
samul-1commented, Jun 22, 2022

Ah, gotcha! Thank you, will give it a try.

Read more comments on GitHub >

github_iconTop Results From Across the Web

foreach - Ghost Handlebars Theme Helpers
{{#foreach}} is a special loop helper designed for working with lists of posts. It can also iterate over lists of tags or users...
Read more >
What does [].forEach.call() do in JavaScript? - Stack Overflow
[] is an array. This array isn't used at all. It's being put on the page, because using an array gives you access...
Read more >
Avoid forEach - ÆFLASH
It is a un-semantic method. There are better iteration functions. There is map , which creates a new array with the results of...
Read more >
Logic Apps foreach and variables - Blog - NETWORG
Sometimes we need to work with a variable inside a loop section. Whether it's a precomputation or just a helper variable.
Read more >
JavaScript ES6: The forEach() Helper - Jonathan Dempsey
It's iteration over an array or list. In ES5, if we wanted to iterate over an array, we would make use of a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found