当前位置:网站首页>uniapp的表单验证
uniapp的表单验证
2022-07-06 19:21:00 【这次我一定要赢】
回顾一下,平常我们在使用element-ui的时候是如何进行表单验证的
<template>
<div class="">
<el-form ref="form" :rules="rules" :model="form">
<el-form-item label="姓名" prop="name">
<el-input v-model="form.name"></el-input>
</el-form-item>
<el-form-item label="年龄" prop="age">
<el-input v-model="form.age"></el-input>
</el-form-item>
</el-form>
<el-button type="primary" @click="fn">保存</el-button>
</div>
</template>
<script>
export default {
data() {
return {
form:{
name:'',
age:''
},
rules:{
name: [{ required: true, message: '必填', trigger: 'blur' }],
age: [{ required: true, message: '必填', trigger: 'blur' }]
}
}
},
name: '',
methods: {
fn(){
this.$refs.form.validate((result)=>{
if (result) {
console.log('验证通过后的逻辑处理');
}
else{
console.log('验证失败后的逻辑处理');
}
})
}
}
}
</script>
<style scoped>
</style>
(1)在el-form上绑定的东西
1.ref获取表单调用viliadte方法做全局的验证
2.:rules 验证规则
3.:model 绑定关联的数据
(2)在el-form-item上绑定东西
1.label关联的是标题
2.prop绑定是关联的字段
(3)在el-input上绑定的东西
el-input,绑定双向绑定要关联的数据
uniapp的表单验证
1.uni-forms需要通过rules属性传入约定的校验规则
<!-- rules 内容详见下方完整示例 -->
<uni-forms ref="form" :rules="rules">
...
</uni-forms>这个和element-ui是一样的
2.uni-forms 需要绑定model属性,值为表单的key\value 组成的对象。
<!-- formData、rules 内容详见下方完整示例 -->
<uni-forms ref="form" :model="formData" :rules="rules">
...
</uni-forms>3.uni-forms-item 需要设置 name 属性为当前字段名,字段为 String|Array 类型。
<!-- formData、rules 内容详见下方完整示例 -->
<uni-forms :modelValue="formData" :rules="rules">
<uni-forms-item label="姓名" name="name">
<uni-easyinput type="text" v-model="formData.name" placeholder="请输入姓名" />
</uni-forms-item>
<uni-forms-item required :name="['data','hobby']" label="兴趣爱好">
<uni-data-checkbox multiple v-model="formData.data.hobby" :localdata="hobby"/>
</uni-forms-item>
</uni-forms>这里的name相当于element-ui里面的prop
4.规则验证
rules: {
// 对name字段进行必填验证
name: {
rules: [{
required: true,
errorMessage: '请输入姓名',
},
{
minLength: 3,
maxLength: 5,
errorMessage: '姓名长度在 {minLength} 到 {maxLength} 个字符',
}
]
},
// 对email字段进行必填验证
email: {
rules: [{
format: 'email',
errorMessage: '请输入正确的邮箱地址',
}]
}
}
这里可以看出来uni多加了一个rules。
其他可以见官网进行查看。
边栏推荐
- MySQL提升大量数据查询效率的优化神器
- The panel floating with the mouse in unity can adapt to the size of text content
- 【2022国赛模拟】多边形——计算几何、二分答案、倍增
- 记一次JAP查询导致OOM的问题分析
- leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]
- 1 -- Xintang nuc980 nuc980 porting uboot, starting from external mx25l
- Statistics of radar data in nuscenes data set
- Derivative, partial derivative, directional derivative
- wireshark安装
- Wireshark installation
猜你喜欢

软件测试——Jmeter接口测试之常用断言

Google Earth Engine(GEE)——Landsat 全球土地调查 1975年数据集

Planning and design of double click hot standby layer 2 network based on ENSP firewall

一文读懂Faster RCNN

Digital scrolling increases effect

C语言练习题_1

C # / vb. Net supprime le filigrane d'un document word

Pioneer of Web3: virtual human

普通测试年薪15w,测试开发年薪30w+,二者差距在哪?

6-6 vulnerability exploitation SSH security defense
随机推荐
Dotconnect for DB2 Data Provider
一本揭秘字节万台节点ClickHouse背后技术实现的白皮书来了!
Convert widerperson dataset to Yolo format
Summary of basic debugging steps of S120 driver
The so-called consumer Internet only matches and connects industry information, and does not change the industry itself
[leetcode]Search for a Range
Unity custom webgl packaging template
Google Earth engine (GEE) -- 1975 dataset of Landsat global land survey
INS/GPS组合导航类型简介
What management points should be paid attention to when implementing MES management system
MMDetection3D加载毫米波雷达数据
[leetcode]Search for a Range
如何设计好接口测试用例?教你几个小技巧,轻松稿定
实施MES管理系统时,哪些管理点是需要注意的
PSINS中19维组合导航模块sinsgps详解(时间同步部分)
导数、偏导数、方向导数
MetaForce原力元宇宙佛萨奇2.0智能合约系统开发(源码部署)
MySQL
慧通编程入门课程 - 2A闯关
How to design interface test cases? Teach you a few tips to draft easily