Vue循环组件加validate多表单验证的实例
网络编程 2021-07-04 16:46www.168986.cn编程入门
今天长沙网络推广就为大家分享一篇Vue循环组件加validate多表单验证的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随长沙网络推广过来看看吧
父父组件(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`, ponents: { PromiseFather }, data () { return { promiseFather: '' } }, methods: { saveAll () { this.$refs.promiseFather.validate(valid => { if (valid) { this.$message.suess('(^o^)~ 验证成功!') } else { this.$message.error('-_- 验证失败!') } }) } } } </script> <style scoped> .button { position: absolute; : 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 '@/ponents/promiseChild' export default { name: `PromiseFather`, ponents: { 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>
以上这篇Vue循环组件加validate多表单验证的实例就是长沙网络推广分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持狼蚁SEO。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程