javascript - 如何将一个 Vue 组件中的所有字段与另一个组件一起验证(使用 Vee-Validate)?

标签 javascript validation vue.js vuejs2 vue-component

我使用 Vue.js 2.5.13 + Vee-Validate 2.0.3。我的代码结构是:

./component-one.vue:

<template>
  <div>

    <input type="text" name="input_one" id="input_one"
           v-model="input_one" 
           v-validate="'required'"
           :class="{'is-danger': errors.has('input_one')}" />

    <component-two></component-two>

    <button @click="submitForm">Submit!</button>

  </div>
</template>

<script>
  import Vue from 'vue'
  import VeeValidate from 'vee-validate'
  import ComponentTwo from './component-two.vue'

  Vue.use(VeeValidate, {
    events: 'input|blur'
  })

  export default {
    name: "component-one",
    components: {
      ComponentTwo
    },
    data() {
      return {
        input_one: '',
      }
    },
    methods: {
      submitForm: function () {
        // Validate before submit form
        this.$validator.validateAll().then((result) => {
          if (result) {
            alert('From Submitted!')
            return
          }
          alert('Correct them errors!')
        })
      }
    }
  }
</script>

./component-two.vue:

<template>
  <div>

    <input type="text" name="input_two" id="input_two"
           v-model="input_two" 
           v-validate="'required'"
           :class="{'is-danger': errors.has('input_two')}" />

  </div>
</template>

<script>
  export default {
    name: "component-two",
    data() {
      return {
        input_two: ''
      }
    }
  }
</script>

当我点击 ComponentOne 中的 button @click="submitForm" 时,如何验证来自 ComponentTwo 的字段(用于保存所有表单数据在这个组件)。

我有一个巨大的表单,由类似的小型 Vue 组件(全部收集在 ComponentOne 中)制作而成,如果能在一个地方验证所有这些组件会很棒。

最佳答案

您可以通过 vue 引用手动触发组件上的 validateAll(),例如:

父组件

<template>
  <div>

    <input type="text" name="input_one" id="input_one"
           v-model="input_one" 
           v-validate="'required'"
           :class="{'is-danger': errors.has('input_one')}" />

    <component-two ref="validateMe"></component-two>

    <button @click="submitForm">Submit!</button>

  </div>
</template>

<script>
  import Vue from 'vue'
  import VeeValidate from 'vee-validate'
  import ComponentTwo from './component-two.vue'

  Vue.use(VeeValidate, {
    events: 'input|blur'
  })

  export default {
    name: "component-one",
    components: {
      ComponentTwo
    },
    data() {
      return {
        input_one: '',
      }
    },
    methods: {
      submitForm: async function () {
        // Validate before submit form
        const result = await this.$validator.validateAll() && await this.$refs.validateMe.$validator.validateAll()
        if (result) {
          alert('From Submitted!')
          return
        }
        alert('Correct them errors!')
      }
    }
  }
</script>

关于javascript - 如何将一个 Vue 组件中的所有字段与另一个组件一起验证(使用 Vee-Validate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48509051/

相关文章:

html - 使用 select2 时保留输入验证 CSS 类

php - 谷歌地理图表上的自定义标签

javascript - 为什么通过 AJAX 下载 zip 文件需要这么长时间?

javascript - 在 Node 或浏览器端 JavaScript 中调整图像大小的好方法是什么?

javascript - 如何在具有类型的 TypeScript 项目中 Vue.use() 一个 javascript 插件

javascript - 使用 for 循环递归地循环对象数组

javascript - 更新 Vuetify slider 上的值

javascript - 使用函数初始化 ng-model 字符串

仅使用计数器对数字范围和数字值进行 Java 输入验证

ruby - 如何验证 Ruby 中多选提示的命令行输入?