我目前正在try 创建一个包含多个"输入字段"组件的注册表,这些组件都需要在按下提交后进行验证.当其中的文本发生更改时,它们都会自动进行验证,但我发现很难对所有输入字段进行全局调用来验证所有内容.我试图实现以下目标:http://vee-validate.logaretm.com/examples#validate-form但validateAll()方法没有附加字段.

我try 用邮箱填充validateAll();确认收到我想要的结果的邮箱,但错误消息不会显示在字段旁边.

任何帮助都将不胜感激!

验证输入字段.vue:

<template>
   <div class="form-control" v-bind:class="{ errorPrompt : errors.has(name) }">
      <label v-bind:for="name">* {{as}}</label>
      <input ref="input" v-bind:value="_value" @input="updateValue($event.target.value)" v-validate v-bind:data-vv-rules="rules" v-bind:name="name" />
      <span v-bind:v-show="'errors.has(' + name +')'">{{ errors.first(name) }}</span>
   </div>
</template>

<script>
module.exports = {
  props: ['name', 'rules', 'as', 'value'],
  methods: {
  updateValue(value) {
     this._value = this.value;
     this.$emit('input', value);
  }
  },
  computed: {
     _value() { return this.value; }
  }
};
</script>

登记.vue:

<template>
  <div class="container">
    <Card blueHeader="true" title="Register">
      <ValidatedInputField v-model="email" name="email" rules="required|email" as="Email" />
      <ValidatedInputField v-model="confirm_email" name="confirm_email" rules="required|email" as="Confirm Email" />
      <ValidatedInputField name="company" rules="required" as="Company" />
      <InputField name="company_website" as="Company Website" />
      <ValidatedInputField name="first_name" rules="required" as="First Name" />
      <ValidatedInputField name="last_name" rules="required" as="Last Name" />
      <ValidatedInputField name="address_1" rules="required" as="Address 1" />
      <InputField name="address_2" as="Address 2" />
      <ValidatedInputField name="city" rules="required" as="City" />
      <ValidatedInputField name="zip" rules="required" as="Zip/Postal Code" />
    </Card>

    <Card blueHeader="true" title="Terms & Conditions">
      <button v-on:click="submitForm()">Submit</button>
    </Card>
  </div>
</template>

<script>
import ValidatedInputField from './components/ValidatedInputField';
import InputField from './components/InputField';

module.exports = {
  methods: {
    submitForm() {
      this.$validator.validateAll();
    }
  },
  data() {
    return {
      email: '',
      confirm_email: ''
    };
  },
  components: {
    ValidatedInputField,
    InputField
  }
};
</script>

推荐答案

我不确定我是否理解正确.但要全局调用,您必须在单击按钮时发出一个事件,并指示每个模板对该事件采取行动.每个模板的操作都应该是"这个"$验证器.validateAll(),因为"this"将引用该特定模板.

您可以通过创建一个命名实例("总线")来实现这一点.在创建实例之前创建它.

var bus = new Vue({});

使用该选项从模板emits :

bus.$emit('validate_all');

要捕捉模板:

created: function() {
   var vm = this;
   bus.$on('validate_all', function() {
      vm.$validator.validateAll()
   });
}

现在,所有字段都应该经过验证,所有错误消息都应该显示出来.

Vue.js相关问答推荐

VUE:带自定义组件的数组输入绑定

Nuxt3 / Vue3 - 当属性更改时如何动态更改渲染组件?

传单 vue3 组件变体打开错误的标记 - 单击时弹出

绑定事件在渲染函数中不起作用

当用户在嵌套路由中时激活链接

如何使用 async/await 在 vue js 中添加标头

Vuetify 扩展面板,面板标题左侧带有图标

CssSyntaxError 使用 Webpack 4 构建未知单词

在Vue cli项目中,Laravel api app放在哪里以及如何在vue js中调用api?

使用带有 v-date-picker 的 new Date() 不起作用

从组件的 内的组件调用方法

Vuejs 2,VUEX,编辑数据时的数据绑定

Nuxt 打开链接到模态

在 v-for 循环中使用 v-model

如何将项目推送到 Vuejs 中数据对象的数组中? Vue 似乎没有关注 .push() 方法

即使在生产模式下构建,VueJS 也会显示开发模式开发模式消息

将登录页面包含在单页应用程序中是否不安全?

Vue.js 单向绑定表单

Vue中的嵌套循环

Vue Router beforeRouteLeave 不会停止子组件