表单校验:vue表单校验提示输入内容提示、提示信息未消失
由于vue2 中监听不到对象属性值变化导致子组件没有收到 formLabel 对象属性;问题出现场景: vue 项目主子组件绑定的form对象为父组件传入的对象;子组件没有同步到父组件对应的对象值导致的;将formRules 对象里面对应的key修改与formLabel 对象的key一致就行;日常开发小问题随笔记录,方便后续遇到查阅。
·
表单校验可能出现问题记录
日常开发小问题随笔记录,方便后续遇到查阅。
表单数据绑定输入值与校验输入绑定的值不一致
问题出现场景:vue form 表单绑定的对象,对应item 绑定的值与自定义校验对象值不一致导致:
<template>
<el-form :model="formLabel" :rules="formRules" label-width="80px">
<el-form-item label="部门" prop="department">
<el-input v-model="formLabel.department"></el-input>
</el-form-item>
<el-form-item label="活动区域" prop="areat">
<el-input v-model="formLabelAlign.activeareat"></el-input>
</el-form-item>
<el-form-item label="活动形式" prop="ruleType">
<el-input v-model="formLabelAlign.activeType"></el-input>
</el-form-item>
</el-form>
</template>
<script setup>
import { reactive, ref } from 'vue';
const formRules = reactive({
department: [{ required: true, message: '请输入部门', trigger: 'blur' }],
areat: [{ required: true, message: '请输入活动区域', trigger: 'blur' }],
ruleType: [{ required: true, message: '请输入活动形式', trigger: 'blur' }],
});
const formLabel = ref({
department: '',
activeareat: '',
activeType: '',
});
</script>
<style lang="less"></style>
将formRules 对象里面对应的key修改与formLabel 对象的key一致就行;
<template>
<el-form :model="formLabel" :rules="formRules" label-width="80px">
<el-form-item label="部门" prop="department">
<el-input v-model="formLabel.department"></el-input>
</el-form-item>
<el-form-item label="活动区域" prop="activeareat">
<el-input v-model="formLabelAlign.activeareat"></el-input>
</el-form-item>
<el-form-item label="活动形式" prop="activeType">
<el-input v-model="formLabelAlign.activeType"></el-input>
</el-form-item>
</el-form>
</template>
<script setup>
import { reactive, ref } from 'vue';
const formRules = reactive({
department: [{ required: true, message: '请输入部门', trigger: 'blur' }],
activeareat: [{ required: true, message: '请输入活动区域', trigger: 'blur' }],
activeType: [{ required: true, message: '请输入活动形式', trigger: 'blur' }],
});
const formLabel = ref({
department: '',
activeareat: '',
activeType: '',
});
</script>
<style lang="less"></style>
表单绑定对象初始没有对应绑定的值
问题出现场景: vue 项目主子组件绑定的form对象为父组件传入的对象; 子组件没有同步到父组件对应的对象值导致的;
父组件:
<template>
<div>
<Childern :project-opt="formLabel"></Childern>
</div>
</template>
<script>
export default {
data() {
return {
formLabel:{}
};
},
created() {
this.formLabel = {
department: '',
activeareat: '',
activeType: '',
}
}
};
</script>
<style lang="less"></style>
子组件:
<template>
<el-form :model="formLabel" :rules="formRules" label-width="80px">
<el-form-item label="部门" prop="department">
<el-input v-model="formLabels.department"></el-input>
</el-form-item>
<el-form-item label="活动区域" prop="activeareat">
<el-input v-model="formLabelAlign.activeareat"></el-input>
</el-form-item>
<el-form-item label="活动形式" prop="activeType">
<el-input v-model="formLabelAlign.activeType"></el-input>
</el-form-item>
</el-form>
</template>
<script>
export default {
props: {
formLabel: {
type: Object,
},
},
data() {
return {
formLabels: this.formLabel,
formRules: {
department: [{ required: true, message: '请输入部门', trigger: 'blur' }],
activeareat: [{ required: true, message: '请输入活动区域', trigger: 'blur' }],
activeType: [{ required: true, message: '请输入活动形式', trigger: 'blur' }],
},
};
},
};
</script>
<style lang="less"></style>
由于vue2 中监听不到对象属性值变化导致子组件没有收到 formLabel 对象属性;将 父组件给 formLabel 赋值改为:
this.$set(this.formLabel, 'department', '')
this.$set(this.formLabel, 'activeareat', '')
this.$set(this.formLabel, 'activeType', '')
问题解决
更多推荐
已为社区贡献1条内容
所有评论(0)