Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate Child component Vue vee-validate

App (parent)

Hi I have these component (Child) TextComponent InfoErrorForm

When I press submit from the parent component App is not validate this form. So I tried to validate with inject $validator in the child component (TextComponent), and provide but not show message error . If you can help me to validate children form inisde parent component. This is my code

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>
like image 286
HalleyRios Avatar asked Oct 12 '18 05:10

HalleyRios


1 Answers

I found solution with this code. In my parent component i add provide and i send the $validator

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

In my child component i received this

inject: ['$validator'],

In my parent component i add this method to invoque validation

 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;

      }
    }

Finally to show error in component info-error I use this code

<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>
like image 51
HalleyRios Avatar answered Oct 21 '22 08:10

HalleyRios