验证子组件Vue vee-validate

时间:2018-10-12 05:03:26

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

应用(父)

嗨,我有这些组件(儿童) TextComponent InfoErrorForm

当我按从父组件App提交时,不验证此表单。因此,我尝试在子组件(TextComponent)中注入$ validator进行验证,并提供但不显示message error。 如果可以帮助我验证inisde父组件的子代,则可以。  这是我的代码

AppComponent

<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>

2 个答案:

答案 0 :(得分:6)

我用这段代码解决 在我的父组件中,我提供了,然后发送了$ 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>

答案 1 :(得分:0)

请在您的子组件中注意this.errors,然后在手表中放入this。$ emit 如下所示:

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

然后将其捕获到您的父组件上,在这里https://vuejs.org/v2/api/#vm-emit