惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyberwarzone
Cyberwarzone
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
The Hacker News
The Hacker News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
D
Docker
H
Hacker News: Front Page
Recent Announcements
Recent Announcements
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Proofpoint News Feed
Security Latest
Security Latest
AI
AI
I
InfoQ
Google DeepMind News
Google DeepMind News
阮一峰的网络日志
阮一峰的网络日志
S
Secure Thoughts
Attack and Defense Labs
Attack and Defense Labs
P
Proofpoint News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Scott Helme
Scott Helme
N
News | PayPal Newsroom
Y
Y Combinator Blog
V
Visual Studio Blog
Latest news
Latest news
大猫的无限游戏
大猫的无限游戏
Microsoft Security Blog
Microsoft Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
WordPress大学
WordPress大学
V
Vulnerabilities – Threatpost
C
Cyber Attacks, Cyber Crime and Cyber Security
TaoSecurity Blog
TaoSecurity Blog
S
Security @ Cisco Blogs
D
DataBreaches.Net
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
Netflix TechBlog - Medium
T
Troy Hunt's Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Heimdal Security Blog
美团技术团队
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Security Affairs
T
Threat Research - Cisco Blogs
Webroot Blog
Webroot Blog
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏

博客园 - givebest

Glarity Summary - 利用 ChatGPT为Bilibili及YouTube视频内容生成摘要 (免费无广告) Nodejs CMS——基于 NestJS/NuxtJS 的完整开源项目 Vue.js + Nuxt.js 项目中使用 Vee-validate 表单校验 又拍云 Node.js 实现文件上传、删除 Docz 用 MDX 写 React UI 组件文档 - givebest Mac 远程连接 Windows - givebest 更优雅的方式: JavaScript 中顺序执行异步函数 Node.js 上传文件 - givebest - 博客园 win10 系统右键菜单不显示文字(只有小图标)修复方法 JavaScript 排序算法(JavaScript sorting algorithms) JavaScript addEventListener 第三个参数 webpack 实现的多入口项目脚手架 Kindle电子阅读器收不到个人文档推送解决方案 纯CSS3大转盘抽奖(响应式、可配置) - givebest - 博客园 JavaScript简单分页,兼容IE6,~3KB 原生JavaScript实现hasClass、addClass、removeClass、toggleClass - givebest - 博客园 JavaScript闭包(Closure) GitHub Pages 绑定二级域名 - givebest HTML5 Canvas绘制转盘抽奖 - givebest - 博客园
Vee-validate 父组件获取子组件表单校验结果
givebest · 2019-05-18 · via 博客园 - givebest

vee-validate 是为 Vue.js 量身打造的表单校验框架,允许您校验输入的内容并显示对应的错误提示信息。它内置了很多常见的校验规则,可以组合使用多种校验规则,大部分场景只需要配置就能实现开箱即用,还支持自定义正则表达式。而且支持 40 多种语言,对本地化、多语言支持非常友好。

国内饿了么团队开源项目 Element UI 就用到了 vee-validate

vee-validate官网:https://baianat.github.io/vee-validate/

使用方法

可查看官网文档(https://baianat.github.io/vee-validate/)或者查看这一篇文章(https://blog.givebest.cn/javascript/2019/04/20/vue.js-nuxt.js-use-vee-validate.html)。

组件内使用 Vee-validate

子组件

<template>
  <div>
    <input
      placeholder="请输入姓名"
      v-model="username"
      name="username"
      v-validate="'required'"
      :error-message="errors.first('username')"
    />
  </div>
</template>

<script>
export default {
  name: "Username",
  data() {
    return {
      username: ''
    }
  },
  methods: {
    // 表单校验
    validateForm() {
      return this.$validator.validateAll();
    },
  }
};
</script>

父组件

<template>
  <div>
    <Username ref="usernameComponent" />
    <Password ref="passwordComponent" />

    <div>
      <button @click="onSubmit">提交校验</button>
    </div>
  </div>
</template>

<script>
import Username from "~/components/username.vue"
import Password from "~/components/password.vue"

export default {
  components: {
    Username,
    Password
  },
  data() {
    return {}
  },
  methods: {
    onSubmit (e) {
      e.preventDefault()  // 阻止默认事件

      // 父组件触发子组件校验,并通过 Promise 返回值
      let vf1 = this.$refs.usernameComponent.validateForm()
      let vf2 = this.$refs.passwordComponent.validateForm()

      // 提交表单前,校验所有子组件,全部通过才允许下面操作
      Promise.all([vf1, vf2]).then(result => {
        // 有一个组件未通过,就提示错误信息
        if (result.indexOf(false) > -1) {
          console.log("全部校验未通过")
          return
        }

        // 校验全部通过处理
        console.log("全部校验通过")
      })
    },
  }
};
</script>

总结

其实组件内使用 Vee-validate 校验很方便,主要问题可能是父组件怎么触发子组件内的校验,并获取校验结果。这里用到 Vue.js 里的 ref 特性,给子组件赋值一个 ID 引用,然后就可以使用 this.$refs.childComponent 获得子组件实例引用,再分别调起子组件写好的校验方法,如:

/**
父组件触发子组件校验,并通过 Promise 返回值
*/
let vf1 = this.$refs.usernameComponent.validateForm() // 父组件调用 usernameComponent 组件里的 validateForm 方法
let vf2 = this.$refs.passwordComponent.validateForm() // 父组件调用 passwordComponent 组件里的 validateForm 方法

然后通过 Promise.all 获取全部子组件校验结果后,再根据结果来判断,是否全部通过,分别做出不同处理。

// 提交表单前,校验所有子组件,全部通过才允许下面操作
Promise.all([vf1, vf2]).then(result => {
  // 有一个组件未通过,就提示错误信息
  if (result.indexOf(false) > -1) {
    console.log("全部校验未通过")
    return
  }

  // 校验全部通过处理
  console.log("全部校验通过")
})

转载请注明出处: https://blog.givebest.cn/javascript/2019/05/18/vee-validate-get-subcomponent-verification-results.html