首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >验证子组件Vue vee-验证

验证子组件Vue vee-验证
EN

Stack Overflow用户
提问于 2018-10-12 13:03:26
回答 2查看 11.9K关注 0票数 10

应用程序(父级)

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

当我从父组件中按下submit时,App不会验证此表单。因此,我尝试在子组件(TextComponent)中使用inject $validator进行验证,并提供但不显示消息错误。如果你能帮助我在父组件中验证子表单。这是我的代码

AppComponent

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-16 03:25:48

我用这段代码找到了解决方案。在父组件中,我添加了provide并发送了$validator

代码语言:javascript
复制
export default {
    components:{
      ...
    },
    provide() {
      return {
        $validator: this.$validator,
      }
    },

在我的子组件中,我收到了以下内容

代码语言:javascript
复制
inject: ['$validator'],

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

代码语言:javascript
复制
 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;

      }
    }

最后,为了显示组件信息中的错误- error,我使用以下代码

代码语言:javascript
复制
<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>
票数 21
EN

Stack Overflow用户

发布于 2018-10-12 14:42:24

在你的子组件中注意this.errors,并在手表中放入this.$emit,如下所示:

代码语言:javascript
复制
watch: {
  errors: function (value) {
    this.$emit('TextComponent', value)
  }
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52772567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档