当前位置:网站首页>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。
其他可以见官网进行查看。
边栏推荐
- QT常见概念-1
- MySQL
- Have fun | latest progress of "spacecraft program" activities
- Statistics of radar data in nuscenes data set
- Wireshark installation
- Ali yunyili: how does yunyuansheng solve the problem of reducing costs and improving efficiency?
- 所谓的消费互联网仅仅只是做行业信息的撮合和对接,并不改变产业本身
- Summary of basic debugging steps of S120 driver
- Convert widerperson dataset to Yolo format
- leetcode:5. Longest palindrome substring [DP + holding the tail of timeout]
猜你喜欢

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

Derivative, partial derivative, directional derivative

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

Ali yunyili: how does yunyuansheng solve the problem of reducing costs and improving efficiency?

What are the characteristics of the operation and maintenance management system

unity 自定义webgl打包模板

Classify the features of pictures with full connection +softmax
Django database (SQLite) basic introductory tutorial

6-6 vulnerability exploitation SSH security defense

wireshark安装
随机推荐
wzoi 1~200
The 8 element positioning methods of selenium that you have to know are simple and practical
[leetcode]Search for a Range
Here comes a white paper to uncover the technology behind Clickhouse, a node with 10000 bytes!
哈希表及完整注释
dotConnect for DB2数据提供者
Digital scrolling increases effect
Redis入门完整教程:客户端管理
QT常见概念-1
MMDetection3D加载毫米波雷达数据
Five reasons for clothing enterprises to deploy MES management system
Redis入門完整教程:問題定比特與優化
C#/VB.NET 删除Word文檔中的水印
[software test] the most complete interview questions and answers. I'm familiar with the full text. If I don't win the offer, I'll lose
换个姿势做运维!GOPS 2022 · 深圳站精彩内容抢先看!
代码调试core-踩内存
ODBC database connection of MFC windows programming [147] (with source code)
CSDN summer camp course project analysis
MySQL
MySQL --- 常用函数 - 字符串函数