Programing

하위 구성 요소 Vue ve-validate 확인

c10106 2022. 4. 26. 21:13
반응형

하위 구성 요소 Vue ve-validate 확인

앱(상위)

안녕 나는 이 구성 요소(하위) TextComponent InfoErrorForm을 가지고 있다.

상위 구성요소로부터 제출을 누르면 앱이 이 양식의 유효성을 검사하지 않는다.그래서 나는 Child 컴포넌트(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>

나는 이 코드로 해결책을 찾았다.나의 부모 구성 요소에서 나는 제공하고 나는 다음을 보낸다.$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>

자녀 구성 요소에서 이 오류를 확인하고, 이 오류를 감시하십시오.$emit 아래 이와 비슷한 것:

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

그런 다음 상위 구성 요소에 연결하십시오. https://vuejs.org/v2/api/#vm-properties

참조URL: https://stackoverflow.com/questions/52772567/validate-child-component-vue-vee-validate

반응형