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.

Unable to validate child components

See original GitHub issue

Versions

  • vee-validate: 2.1.0-beta2
  • vue: 2.5.2

Describe the bug I have an application with several child components all of which have

inject: ['$validator']

the parent’s code is this:


<template>
  <form id="myform">
    <CompA />
    <CompB />
    <CompC />
    <CompD />
  </form>
</template>

<script>
import { eventBus } from '@/main'

const CompA = () => import('@/components/CompA')
const CompB = () => import('@/components/CompB')
const CompC = () => import('@/components/CompC')
const CompD = () => import('@/components/CompD')

export default {
  name: 'MyForm',
  inject: ['$validator'],
  data() {
    return {

    }
  },
  components: {
    CompA,
    CompB,
    CompC,
    CompD
  },
  methods: {
    submit() {
      this.$children.forEach(vm => {
        console.log(vm.$options._componentTag)
        vm.$validator.validateAll().then(result => {
          if (!result) {
            console.log('el: '+ vm.$validator.errors.items[0].field)
            this.$el.querySelector('#' + vm.$validator.errors.items[0].field).scrollIntoView(false)
          }
        }).catch((err) => {
          console.log('error: ' + err)
        })
      })
    }
  }
}
</script>

The _componentTag is correct in that it detects components A through D but the problem is that the validation is only working on the element of CompD. $validator.errors does not show anything else. I would expect all the required fields from CompA…CompC to show up as well.

Am I validating something incorrectly or is validation skipping children component?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
logaretmcommented, Jul 13, 2018

v-if removes the elements from the DOM, vee-validate doesn’t validate any fields hidden by v-if since the directive won’t execute. You can use v-show if the form has multiple tabs has a similar structure. This will allow you to maintain the fields state until the form finally submits.

If you are building something like a form with steps then it should work fine with v-if or v-show regardless since each step would be required to be correct before proceeding, in that case. Persisting the validation state for each step is redundant.

0reactions
echodromecommented, Jul 13, 2018

Thank you @logaretm

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optionally do not validate child components #719 - GitHub
Now when I am validating parent component it says it's invalid because of failing child rules. It would be nice to have an...
Read more >
Unable to validate a child component from a parent component
I have a selectbox component. I want to reused it in other components.I'm able to reused the selectbox component in other components by ......
Read more >
Child Component Validation - CodeSandbox
The Vue build version to load with the `import` command. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
Read more >
Validation of child component | Blazor Forums - Syncfusion
Hi,. I have created the child component with the textbox, but the validation inside the component does not work. Some time ago you...
Read more >
How to fire validation error for input field from child component ...
Create @api method to validate child components text input field. Here you can use template.queryselector to access the value of the text ...
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