【问题标题】:Validate Child component Vue vee-validate验证子组件Vue vee-validate
【发布时间】:2018-10-12 05:03:26
【问题描述】:

应用(父级)

嗨,我有这些组件(儿童) 文本组件 信息错误表单

当我从父组件按提交时,应用程序不会验证此表单。所以我尝试在子组件(TextComponent)中使用注入 $validator 进行验证,并提供但不显示消息错误。 如果您可以帮助我验证子窗体 inisde 父组件。 这是我的代码

应用组件

<template>
      <div>
        <!-- Form validar numero max input -->
        <form :class="{'was-validated': error_in_form_save_progress}" >

           <card-shadow v-for="(texto,key) in sections_template.texts" :key="key" > 
            <texto-component 
              :orden="key+2"
              v-model="sections_template.texts[key].content" 
              :tituloComponente="texto.title" 
              :inputName="texto.title" >
              <template slot="section_show_error_validate_input">
                <info-error-form 
                  :listErrors='errors' 
                  :name_field = "texto.title" 
                  v-show = "error_in_form_save_progress" >
                </info-error-form>
              </template>
            </texto-component>
          </card-shadow>

        </form>

        <div class="row foot_button" >
          <div class="offset-md-3 col-md-3">
            <button class="btn" @click.prevent="save_progrees"> Guardar Cambios</button>
          </div>

        </div>
      </div>
    </template>

    <script>

      export default {

        provide() {
       return {
         $validator: this.$validator,
       };
    },
        data: function(){
          return {
            sections_template: {
              texts:[
                {
                  section_template_id: 1,
                  type: "texto",
                  title: "fundamentacion",
                  content: ""
                },
                {
                  section_template_id: 2,
                  type: "texto",
                  title: "sumilla",
                  content: ""
                }
            ] },
            error_in_form_save_progress: true
          }
        },
        methods:{
          save_progrees(){
             this.$validator.validateAll().then((result) => {
            if (result) {
             this.error_in_form_save_progress = false;
             alert("se guardaran cambios");
             return
            }
            this.error_in_form_save_progress = true;
            });

          }
        }
      }
    </script>

【问题讨论】:

    标签: vue.js vuejs2 vue-component vee-validate


    【解决方案1】:

    我找到了这个代码的解决方案。在我的父组件中,我添加提供并发送$validator

    export default {
        components:{
          ...
        },
        provide() {
          return {
            $validator: this.$validator,
          }
        },
    

    在我的子组件中我收到了这个

    inject: ['$validator'],
    

    在我的父组件中,我将此方法添加到调用验证

     methods:{
          save_progrees(){
            var validationArray = this.$children.map(function(child){
                return child.$validator.validateAll();
            });
    
            window.Promise.all(validationArray).then((v) => {
              v.some( element => { if ( element == false ) { throw "exists error in child component";} });
              this.error_in_form_save_progress = false;
              alert("datos guardados");
              return
            }).catch(() => {
              this.show_message_error_validation();
            });
    
          },
          show_message_error_validation(){
            this.error_in_form_save_progress = true;
    
          }
        }
    

    最后在组件信息错误中显示错误我使用此代码

    <template>
      <div class="row" v-if="errors.items">
        <div class="col-md-12">
          <template v-for="(e,key) in errors.items"  >
            <div  class="text-danger"  v-if="e.field ==name_field" :key="key"> {{e.msg}}  </div> 
          </template>
        </div>
      </div>
    </template>
    

    【讨论】:

    • 很好地解释了答案。使用this.$validator.validate().then(valid =&gt; {doSomething(valid)});时我也不需要修改我的验证函数
    【解决方案2】:

    在您的子组件中注意 this.errors 并在手表中输入 this.$emit 下面是这样的:

    watch: {
      errors: function (value) {
        this.$emit('TextComponent', value)
      }
    }
    

    然后在你的父组件上捕获它在这里看到它https://vuejs.org/v2/api/#vm-emit

    【讨论】:

    • 您好,我将此代码放在我的子组件中但无法正常工作,错误消息未显示
    • 把 console.log(value) 放在那个手表里。在您进行无效输入后查看值是否更改。你把听众放在你的父母身上了吗?请参阅我放在那里的文档的网址..
    猜你喜欢
    • 2020-01-30
    • 2018-03-01
    • 2020-12-20
    • 2020-02-29
    • 2017-09-04
    • 2020-11-21
    • 2021-05-06
    • 2021-03-08
    • 2017-04-22
    相关资源
    最近更新 更多