针对el-form的数据源是对象嵌套对象,在进行数据绑定和校验时和单层的对象有一点区别,
具体是下面两部分:
数据源:
fromData: {
name: '',
health: {
height: ''
}
}
1、 给 el-form-item
的 prop
设为:prop="health.height"
。v-model
设为: v-model="fromData.health.height"
<el-form-item label="身高" prop="health.height">
<el-input v-model="fromData.health.height" clearable />
</el-form-item>
2、校验规则rules
对象对应的key设置为数据源内部的值: 'health.height'
文章来源:https://www.toymoban.com/news/detail-623764.html
rules: {
'health.height': [{ required: true, message: '请输入身高', trigger: 'blur' }]
}
具体代码文章来源地址https://www.toymoban.com/news/detail-623764.html
<template>
<div>
<el-form ref="fromRef" :model="fromData" :rules="rules">
<el-form-item label="姓名" prop="name">
<el-input v-model="fromData.name" clearable />
</el-form-item>
<el-form-item label="身高" prop="health.height">
<el-input v-model="fromData.health.height" clearable />
</el-form-item>
</el-form>
<el-button @click="onSubmit()">提交</el-button>
</div>
</template>
<script>
export default {
data() {
return {
fromData: {
name: '',
health: {
height: ''
}
},
rules: {
name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
'health.height': [{ required: true, message: '请输入身高', trigger: 'blur' }]
}
}
},
methods: {
onSubmit() {
this.$refs['fromRef'].validate((valid) => {
if (valid) {
console.log('校验通过')
console.log('fromData', JSON.stringify(this.fromData))
}
})
}
}
}
</script>
<style scoped></style>
到了这里,关于Vue - Element el-form 表单对象多层嵌套校验的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!