当前位置:网站首页>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。
其他可以见官网进行查看。
边栏推荐
- Redis入门完整教程:客户端管理
- STM32 project -- Topic sharing (part)
- KYSL 海康摄像头 8247 h9 isapi测试
- Matlb| economic scheduling with energy storage, opportunity constraints and robust optimization
- Use of fiddler
- Web3's need for law
- 服装企业部署MES管理系统的五个原因
- Unity webgl adaptive web page size
- Here comes a white paper to uncover the technology behind Clickhouse, a node with 10000 bytes!
- 从控制理论的角度谈数据分析
猜你喜欢
MySQL - common functions - string functions
服装企业部署MES管理系统的五个原因
C#/VB. Net to delete watermarks in word documents
Increase 900w+ playback in 1 month! Summarize 2 new trends of top flow qiafan in station B
Web3's need for law
Cloud Mail . NET Edition
Planning and design of double click hot standby layer 2 network based on ENSP firewall
What are the applications and benefits of MES management system
Hash table and full comments
AWS学习笔记(一)
随机推荐
LeetCode 77:组合
Code line breaking problem of untiy text box
Increase 900w+ playback in 1 month! Summarize 2 new trends of top flow qiafan in station B
Rethinking of investment
CDB PDB user rights management
Fundamentals of process management
Software testing -- common assertions of JMeter interface testing
数字滚动增加效果
Cloud Mail . NET Edition
Remember the problem analysis of oom caused by a Jap query
The 8 element positioning methods of selenium that you have to know are simple and practical
差异与阵列和阵列结构和链表的区别
Classify the features of pictures with full connection +softmax
Argo workflows source code analysis
Application analysis of face recognition
Go swagger use
Five reasons for clothing enterprises to deploy MES management system
Qpushbutton- "function refinement"
How to build a 32core raspberry pie cluster from 0 to 1
Read fast RCNN in one article