Vue循环组件加validate多表单验证的实例
作者:IH1107 时间:2024-05-05 09:23:39
*父父组件(helloWorld.vue):
<template>
<div class="hello-world">
<el-button type="text" @click="saveAll" class="button">SAVE</el-button>
<promise-father ref="promiseFather"></promise-father>
</div>
</template>
<script>
import PromiseFather from './promiseFather'
export default {
name: `HelloWorld`,
components: { PromiseFather },
data () {
return {
promiseFather: ''
}
},
methods: {
saveAll () {
this.$refs.promiseFather.validate(valid => {
if (valid) {
this.$message.success('(^o^)~ 验证成功!')
} else {
this.$message.error('-_- 验证失败!')
}
})
}
}
}
</script>
<style scoped>
.button {
position: absolute;
top: 60px;
left: 660px;
}
</style>
*父组件(promiseFather.vue):
<template>
<div class="promise-father">
<h2>{{ title }}
<el-button type="text" @click="add" class="button">添加</el-button>
</h2>
<div v-for="(item, index) in validateSet" :key="item.id">
<promise-child ref="promiseChild" :formData="item" :index="index" @remove="remove"></promise-child>
</div>
</div>
</template>
<script>
import uuid from 'uuid'
import PromiseChild from '@/components/promiseChild'
export default {
name: `PromiseFather`,
components: {
PromiseChild
},
data () {
return {
title: 'WHY! mtw',
promiseChild: '',
validateSet: []
}
},
methods: {
validate (callback) {
if (this.validateSet && this.validateSet.length > 0) {
const promiseList = []
this.$refs.promiseChild.forEach((item, index) => {
promiseList.push(item.validate())
})
Promise.all(promiseList).then(() => {
callback(true)
}).catch(() => {
callback(false)
})
} else {
callback(true)
}
},
add () {
this.validateSet.push({
name: '',
phone: '',
id: uuid.v4()
})
},
remove (num) {
this.validateSet.splice(num, 1)
}
}
}
</script>
<style scoped>
.index {
margin-left: -546px;
}
.button {
margin-left: 60px;
}
</style>
*子组件(promiseChild.vue):
<template>
<div class="promise-child">
<el-form :model="form" ref="form" :rules="formRules" :inline="true" label-position="right">
<el-form-item :label="`${index + 1}`+'、'">
</el-form-item>
<el-form-item label="姓名" prop="name">
<el-input v-model="form.name" size="small"></el-input>
</el-form-item>
<el-form-item label="手机" prop="phone">
<el-input v-model="form.phone" size="small"></el-input>
</el-form-item>
<el-form-item>
<el-button @click="remove" size="small">删除</el-button>
</el-form-item>
</el-form>
</div>
</template>
<script>
import uuid from 'uuid'
export default {
name: `PromiseChild`,
props: {
formData: Object,
index: Number
},
data () {
return {
form: {
name: '',
phone: '',
id: uuid.v4()
},
formRules: {
name: [
{ required: true, message: '请输入姓名!', trigger: 'blur' }
],
phone: [
{ required: true, message: '请输入手机号码', trigger: 'blur' },
{ max: 11, message: '长度不能超过11', trigger: 'blur' },
{
validator: (rules, value, callback) => {
if (value) {
let regPhone = /^(13[0-9]|15[012356789]|18[0123456789]|147|145|17[0-9])\d{8}$/
if (regPhone.test(value)) {
callback()
} else {
callback('请输入正确的手机号码!')
}
} else {
callback('请输入手机号码!')
}
}
}
]
}
}
},
methods: {
validate () {
return new Promise((resolve, reject) => {
this.$refs.form.validate(valid => {
if (valid) {
resolve()
} else {
reject()
}
})
})
},
remove () {
this.$emit('remove', this.index)
}
}
}
</script>
来源:https://blog.csdn.net/IH1107/article/details/79170427
标签:Vue,validate,表单,验证
0
投稿
猜你喜欢
SQL学习笔记四 聚合函数、排序方法
2024-01-20 02:54:43
Element-ui el-tree新增和删除节点后如何刷新tree的实例
2023-07-02 17:03:52
Pytorch自定义Dataset和DataLoader去除不存在和空数据的操作
2022-05-06 14:26:39
python 下载文件的几种方式分享
2021-03-27 14:08:17
python非标准时间的转换
2022-04-12 17:37:52
Go基础教程系列之import导入包(远程包)和变量初始化详解
2024-02-22 03:33:53
python迭代器常见用法实例分析
2023-07-12 02:40:54
js与jquery获取父级元素,子级元素,兄弟元素的实现方法
2024-05-11 09:43:01
python文件和目录操作方法大全(含实例)
2021-11-11 14:10:29
MySQL修改配置,区分大小写
2010-12-03 16:31:00
JS Object.preventExtensions(),Object.seal()与Object.freeze()用法实例分析
2023-09-04 22:44:33
JS密码生成与强度检测完整实例(附demo源码下载)
2024-04-25 13:13:07
python乱序字符串排序的实现方式
2023-08-02 12:14:59
Python BeautifulReport可视化报告代码实例
2023-11-12 14:53:13
对Python 多线程统计所有csv文件的行数方法详解
2021-09-09 04:56:44
提高ASP性能的最佳选择第一部分
2007-08-15 12:31:00
python同时遍历两个list用法说明
2023-10-12 23:08:23
吴恩达机器学习练习:SVM支持向量机
2023-10-30 11:49:53
Python求解平方根的方法
2023-02-13 13:25:47
浏览器右下角弹出提示窗口
2008-10-30 12:37:00