常规表单验证、自定义表单验证、动态增删表单验证
1.常规表单验证
image.png<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
<el-form-item label="活动名称" prop="name">
<el-input v-model="ruleForm.name"></el-input>
</el-form-item>
<el-form-item label="活动区域" prop="region">
<el-select v-model="ruleForm.region" placeholder="请选择活动区域">
<el-option label="区域一" value="shanghai"></el-option>
<el-option label="区域二" value="beijing"></el-option>
</el-select>
</el-form-item>
<el-form-item label="活动时间" required>
<el-col :span="11">
<el-form-item prop="date1">
<el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.date1" style="width: 100%;"></el-date-picker>
</el-form-item>
</el-col>
<el-col class="line" :span="2">-</el-col>
<el-col :span="11">
<el-form-item prop="date2">
<el-time-picker placeholder="选择时间" v-model="ruleForm.date2" style="width: 100%;"></el-time-picker>
</el-form-item>
</el-col>
</el-form-item>
<el-form-item label="即时配送" prop="delivery">
<el-switch v-model="ruleForm.delivery"></el-switch>
</el-form-item>
<el-form-item label="活动性质" prop="type">
<el-checkbox-group v-model="ruleForm.type">
<el-checkbox label="美食/餐厅线上活动" name="type"></el-checkbox>
<el-checkbox label="地推活动" name="type"></el-checkbox>
<el-checkbox label="线下主题活动" name="type"></el-checkbox>
<el-checkbox label="单纯品牌曝光" name="type"></el-checkbox>
</el-checkbox-group>
</el-form-item>
<el-form-item label="特殊资源" prop="resource">
<el-radio-group v-model="ruleForm.resource">
<el-radio label="线上品牌商赞助"></el-radio>
<el-radio label="线下场地免费"></el-radio>
</el-radio-group>
</el-form-item>
<el-form-item label="活动形式" prop="desc">
<el-input type="textarea" v-model="ruleForm.desc"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
<el-button @click="resetForm('ruleForm')">重置</el-button>
</el-form-item>
</el-form>
<script>
export default {
data() {
return {
ruleForm: {
name: '',
region: '',
date1: '',
date2: '',
delivery: false,
type: [],
resource: '',
desc: ''
},
rules: {
name: [
{ required: true, message: '请输入活动名称', trigger: 'blur' },
{ min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
],
region: [
{ required: true, message: '请选择活动区域', trigger: 'change' }
],
date1: [
{ type: 'date', required: true, message: '请选择日期', trigger: 'change' }
],
date2: [
{ type: 'date', required: true, message: '请选择时间', trigger: 'change' }
],
type: [
{ type: 'array', required: true, message: '请至少选择一个活动性质', trigger: 'change' }
],
resource: [
{ required: true, message: '请选择活动资源', trigger: 'change' }
],
desc: [
{ required: true, message: '请填写活动形式', trigger: 'blur' }
]
}
};
},
methods: {
submitForm(formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
alert('submit!');
} else {
console.log('error submit!!');
return false;
}
});
},
resetForm(formName) {
this.$refs[formName].resetFields();
}
}
}
</script>
2.自定义表单验证
image.png<el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
<el-form-item label="密码" prop="pass">
<el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="确认密码" prop="checkPass">
<el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="年龄" prop="age">
<el-input v-model.number="ruleForm.age"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
<el-button @click="resetForm('ruleForm')">重置</el-button>
</el-form-item>
</el-form>
<script>
export default {
data() {
var checkAge = (rule, value, callback) => {
if (!value) {
return callback(new Error('年龄不能为空'));
}
setTimeout(() => {
if (!Number.isInteger(value)) {
callback(new Error('请输入数字值'));
} else {
if (value < 18) {
callback(new Error('必须年满18岁'));
} else {
callback();
}
}
}, 1000);
};
var validatePass = (rule, value, callback) => {
if (value === '') {
callback(new Error('请输入密码'));
} else {
if (this.ruleForm.checkPass !== '') {
this.$refs.ruleForm.validateField('checkPass');
}
callback();
}
};
var validatePass2 = (rule, value, callback) => {
if (value === '') {
callback(new Error('请再次输入密码'));
} else if (value !== this.ruleForm.pass) {
callback(new Error('两次输入密码不一致!'));
} else {
callback();
}
};
return {
ruleForm: {
pass: '',
checkPass: '',
age: ''
},
rules: {
pass: [
{ validator: validatePass, trigger: 'blur' }
],
checkPass: [
{ validator: validatePass2, trigger: 'blur' }
],
age: [
{ validator: checkAge, trigger: 'blur' }
]
}
};
},
methods: {
submitForm(formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
alert('submit!');
} else {
console.log('error submit!!');
return false;
}
});
},
resetForm(formName) {
this.$refs[formName].resetFields();
}
}
}
</script>
3.动态增减
a.表单
image.png
<el-form :model="storeForm" :rules="rules" ref="storeForm" label-width="100px">
<el-form-item label="联系电话:" required>
<el-row :gutter="20" class="tel-wrapper">
<el-col :span="6" v-for="(contact,index) in storeForm.storeContacts" :key="contact.id">
<el-form-item :prop="'storeContacts.' + index+ '.mobile'" :rules="rules.mobile">
<el-input v-model="contact.mobile" type="number">
<el-button slot="append" :icon="index == 0 ? 'el-icon-plus' : 'el-icon-delete'" @click="telInputHandler(index)"></el-button>
</el-input>
</el-form-item>
</el-col>
</el-row>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('storeForm')">提交</el-button>
<el-button @click="resetForm('storeForm')">重置</el-button>
</el-form-item>
</el-form>
<script>
import { isMobile } from '@/util/validate'
export default {
data() {
return {
storeForm: {
storeContacts: []
},
rules: {
mobile: { validator: mobile, trigger: 'blur' }
}
};
},
methods: {
submitForm(formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
alert('submit!');
} else {
console.log('error submit!!');
return false;
}
});
},
resetForm(formName) {
this.$refs[formName].resetFields();
}
}
}
</script>
// util/validate.js
export function isMobile (rule:any, value:any, callback:any) {
if (!value) {
callback(new Error('请输入手机号'))
} else {
if (!(/^1[3456789]\d{9}$/.test(value))) {
callback(new Error('手机号码有误'))
} else {
callback()
}
}
}
b.表格
image.png
<el-form ref="cardForm" :model="cardForm" :rules="rules">
<el-row>
<el-col :span="12">
<el-form-item label="会员卡名称:" prop="name" label-width="125px">
<el-input
v-model="cardForm.name"
autocomplete="off"
maxlength="30"
placeholder="输入会员卡名称"
></el-input>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="售价:" prop="price" label-width="125px">
<el-input v-model="cardForm.price" autocomplete="off" type="number" min="0" max="99999">
<template slot="append">元</template>
</el-input>
</el-form-item>
</el-col>
</el-row>
<div style="margin-top:7px">
<p class="sub-title">会员卡内容</p>
<el-table
border
:data="cardForm.contents"
tooltip-effect="dark"
style="width: 100%"
max-height="150"
class="mb-10"
>
<el-table-column label="类型" align="center">
<template slot-scope="scope">
<el-form-item :prop="'contents.' + scope.$index + '.type'" :rules="rules.type">
<el-select
v-model="scope.row.type"
placeholder="请选择"
@change="typeChanged($event,scope.row)"
>
<el-option
v-for="item in categoryOptions"
:key="item.value"
:label="item.label"
:value="item.value"
></el-option>
</el-select>
</el-form-item>
</template>
</el-table-column>
<el-table-column label="内容" align="center">
<template slot-scope="scope">
<span v-if="scope.row.type == 5">现金</span>
<el-form-item
v-if="scope.row.type == 1"
:prop="'contents.' + scope.$index + '.itemId'"
:rules="rules.project"
>
<el-select
v-model="scope.row.itemId"
filterable
placeholder="请选择项目"
@change="nameChanged($event,scope.row)"
>
<el-option
v-for="item in projectList"
:key="item.id"
:label="item.name"
:value="item.id"
></el-option>
</el-select>
</el-form-item>
</template>
</el-table-column>
<el-table-column label="数量/金额" align="center">
<template slot-scope="scope">
<el-form-item :prop="'contents.' + scope.$index + '.amount'" :rules="rules.amount">
<el-input v-model="scope.row.amount"></el-input>
</el-form-item>
</template>
</el-table-column>
<el-table-column label="操作" align="center">
<template slot-scope="scope">
<el-button type="text" icon="el-icon-delete" @click="delRow(scope.row)"></el-button>
</template>
</el-table-column>
</el-table>
<el-button class="add-btn" icon="el-icon-plus" @click="addRow()">继续添加</el-button>
</div>
<el-form-item>
<el-button type="primary" @click="submitForm('cardForm')">提交</el-button>
<el-button @click="resetForm('cardForm')">重置</el-button>
</el-form-item>
</el-form>
<script>
import { isMobile } from '@/util/validate'
export default {
data() {
return {
cardForm: {
name: '',
price: '',
contents: [{}]
},
rules: {
name: { required: true, message: "请输入会员卡名称", trigger: "blur" },
price: { required: true, message: "请输入售价", trigger: "blur" },
type: { required: true, message: "请选择类型", trigger: "blur" },
project: { required: true, message: "请选择项目", trigger: "blur" },
amount: { required: true, message: "请输入数量", trigger: "blur" }
}
};
},
methods: {
addRow () {
let item: any = {}
this.cardForm.contents.push(item)
}
delRow (row: any) {
let tableData = this.cardForm.contents
if (tableData.length === 1) {
return
}
for (var i = 0; i < tableData.length; i++) {
if (tableData[i] === row) {
tableData.splice(i, 1)
}
}
}
submitForm(formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
alert('submit!');
} else {
console.log('error submit!!');
return false;
}
});
},
resetForm(formName) {
this.$refs[formName].resetFields();
}
}
}
</script>
网友评论