优化表单赋值与验证

This commit is contained in:
Gary Fu
2024-01-13 12:25:39 +08:00
parent b685b6b7fa
commit 11f9f92fad
2 changed files with 18 additions and 19 deletions

View File

@@ -1,5 +1,5 @@
<script setup>
import { computed, ref } from 'vue'
import { computed, ref, watch } from 'vue'
import { $i18nBundle } from '@/messages'
import ControlChild from '@/components/common-form-control/control-child.vue'
import { useInputType } from '@/components/utils'
@@ -100,6 +100,20 @@ const rules = computed(() => {
return _rules
})
const initFormModel = () => {
if (formModel.value) {
const option = props.option
if (option.prop) {
const defaultVal = get(formModel.value, option.prop)
set(formModel.value, option.prop, defaultVal || option.value || undefined)
}
}
}
initFormModel()
watch(() => props.option, initFormModel, { deep: true })
</script>
<template>

View File

@@ -1,7 +1,6 @@
<script setup>
import { ref, watch } from 'vue'
import { ref } from 'vue'
import { useVModel } from '@vueuse/core'
import { set } from 'lodash'
/**
* @type {CommonFormProps}
@@ -60,27 +59,13 @@ const props = defineProps({
}
})
//= ============form暴露============//
const form = ref()
const emit = defineEmits(['submitForm', 'update:model'])
const formModel = useVModel(props, 'model', emit)
const initFormModel = () => {
if (formModel.value) {
props.options.forEach(option => {
if (option.prop) {
set(formModel.value, option.prop, option.value || undefined)
}
})
}
}
//= ============form暴露============//
initFormModel()
watch(() => props.options, initFormModel, { deep: true })
const form = ref()
defineExpose({
form