当前位置:网站首页>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。
其他可以见官网进行查看。
边栏推荐
- 数字滚动增加效果
- C#/VB. Net to delete watermarks in word documents
- Cloud Mail .NET Edition
- Increase 900w+ playback in 1 month! Summarize 2 new trends of top flow qiafan in station B
- Summary of basic debugging steps of S120 driver
- 【软件测试】最全面试问题和回答,全文背熟不拿下offer算我输
- [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
- 安全巡检的工作
- Fundamentals of process management
- Go swagger use
猜你喜欢
随机推荐
Redis入門完整教程:問題定比特與優化
测试优惠券要怎么写测试用例?
Redis入门完整教程:AOF持久化
实施MES管理系统时,哪些管理点是需要注意的
PSINS中19维组合导航模块sinsgps详解(初始赋值部分)
MySQL --- 常用函数 - 字符串函数
安德鲁斯—-多媒体编程
The so-called consumer Internet only matches and connects industry information, and does not change the industry itself
Derivative, partial derivative, directional derivative
哈希表及完整注释
The annual salary of general test is 15W, and the annual salary of test and development is 30w+. What is the difference between the two?
MySQL
Redis入门完整教程:客户端常见异常
widerperson数据集转化为YOLO格式
Software testing -- common assertions of JMeter interface testing
CDB PDB 用户权限管理
Redis入门完整教程:客户端案例分析
Draco - gltf model compression tool
用全连接+softmax对图片的feature进行分类
Django database (SQLite) basic introductory tutorial








