Vee-validate 父组件获取子组件表单校验结果
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
Vee-validate 父组件获取子组件表单校验结果的更多相关文章
- vue父组件获取子组件页面的数组 以城市三级联动为例
父组件调用子组件 <Cselect ref="registerAddress"></Cselect> import Cselect from '../../ ...
- 关于Vue中,父组件获取子组件的数据(子组件调用父组件函数)的方法
1. 父组件调用子组件时,在调用处传给子组件一个方法 :on-update="updateData" 2. 子组件在props中,接收这个方法并声明 props: { onUp ...
- Angular5 父组件获取子组件实例( ViewChildren、ViewChild用法)
原文链接 Understanding ViewChildren, ContentChildren, and QueryList in Angular 使用场景 有时候,我们想要在父组件中访问它的子组件 ...
- 子组件获取父组件数据 propsDown, 父组件获取子组件数据 eventUp
(一) popsDowm 三种方法获取父组件数据:被动获得(1):主动获取(2). 1.被动获得: 父组件:v-bind: 绑定变量参数和方法参数:子组件:props 接收参数.可以在模板中直接使用也 ...
- React Hook父组件获取子组件的数据/函数
我们知道在react中,常用props实现子组件数据到父组件的传递,但是父组件调用子组件的功能却不常用.文档上说ref其实不是最佳的选择,但是想着偷懒不学redux,在网上找了很多教程,要不就是hoo ...
- antd 父组件获取子组件中form表单的值
还是拿代码来讲吧,详情见注释 子组件 import React, { Component } from 'react'; import { Form, Input } from 'antd'; con ...
- Promise实现子组件的多表单校验并反馈结果给父组件
全手打原创,转载请标明出处:https://www.cnblogs.com/dreamsqin/p/11529207.html,多谢,=.=~ 本文中多表单验证主要用到Promise.all()实现多 ...
- vue:父子组件间通信,父组件调用子组件方法进行校验子组件的表单
参考: ElementUI多个子组件表单的校验管理:https://www.jianshu.com/p/541d8b18cf95 Vue 子组件调用父组件方法总结:https://juejin.im/ ...
- vue 的父组件和子组件互相获取数据和方法
父组件主动获取子组件的数据和方法 一.ref(但不能实时更新获取) 1.调用子组件的时候 定义一个ref <child ref="headerChild"></c ...
随机推荐
- Python爬虫-- selenium库
selenium库 selenium 是一套完整的web应用程序测试系统,包含了测试的录制(selenium IDE),编写及运行(Selenium Remote Control)和测试的并行处理(S ...
- Android笔记之引用aar
把要引用的aar文件复制到目录app\libs中(我要引用的aar名为xybigdatasdk-release-out2.2.6.aar) 在build.gradle (Module: app)中添加 ...
- pip 安装模块报错解决
系统版本 ubuntu Kylin 16.04 LTS 报错1:安装pip3 安装 Django 总是提示time out,无法安装. 改用国内源: 豆瓣源: sudo pip3 install - ...
- windows10怎么开机启动虚拟机
将如下脚本添加到windows计划任务中即可 "D:\Program Files (x86)\VMware\VMware Workstation\vmplayer.exe" &qu ...
- 系统常用VC++运行时下载地址
Microsoft Visual C++ 2005 Microsoft Visual C++ 2005 Redistributable Package (x86) https://www.micro ...
- spring框架IOC原理分析代码
模拟ClasspathXmlApplication: package junit.test; import java.beans.Introspector; import java.beans.Pro ...
- certbot申请SSL证书及中间证书问题
首先是到https://certbot.eff.org/上申请证书,由于我们使用的web服务器是基于erlang的cowboy的,在主页上没有选项可以支持,因此在Software下拉项中选择" ...
- 多线程mtr-代码
#!/bin/env python # -*- coding: utf-8 -*- # @Date : 2015-09-06 11:30:48 # @Author : Your Name (you@e ...
- CodeForces - 660F:Bear and Bowling 4(DP+斜率优化)
Limak is an old brown bear. He often goes bowling with his friends. Today he feels really good and t ...
- python爬虫知识点总结(二)爬虫的基本原理
一.什么是爬虫? 答:请求网页并提取数据的自动化程序. 二.爬虫的基本流程 三.什么是Request和Response? 1.Request 2.Response 四.能抓取怎样的数据 五.解析方式 ...