fix(Form)!: include nested state in submit data (#3028)

This commit is contained in:
Romain Hamel
2025-01-14 10:49:39 +01:00
committed by GitHub
parent 865a47f125
commit de9ecb1d76
4 changed files with 21 additions and 9 deletions

View File

@@ -60,7 +60,7 @@ const parentBus = inject(
provide(formBusInjectionKey, bus)
const nestedForms = ref<Map<string | number, { validate: () => any }>>(new Map())
const nestedForms = ref<Map<string | number, { validate: typeof _validate }>>(new Map())
onMounted(async () => {
bus.on(async (event) => {
@@ -121,12 +121,12 @@ async function getErrors(): Promise<FormErrorWithId[]> {
return resolveErrorIds(errs)
}
async function _validate(opts: { name?: string | string[], silent?: boolean, nested?: boolean } = { silent: false, nested: true }): Promise<T | false> {
async function _validate(opts: { name?: string | string[], silent?: boolean, nested?: boolean, transform?: boolean } = { silent: false, nested: true, transform: false }): Promise<T | false> {
const names = opts.name && !Array.isArray(opts.name) ? [opts.name] : opts.name as string[]
const nestedValidatePromises = !names && opts.nested
? Array.from(nestedForms.value.values()).map(
({ validate }) => validate().then(() => undefined).catch((error: Error) => {
({ validate }) => validate(opts).then(() => undefined).catch((error: Error) => {
if (!(error instanceof FormValidationException)) {
throw error
}
@@ -151,13 +151,17 @@ async function _validate(opts: { name?: string | string[], silent?: boolean, nes
errors.value = await getErrors()
}
const childErrors = (await Promise.all(nestedValidatePromises)).filter(val => val)
const childErrors = (await Promise.all(nestedValidatePromises)).filter(val => val !== undefined)
if (errors.value.length + childErrors.length > 0) {
if (opts.silent) return false
throw new FormValidationException(formId, errors.value, childErrors)
}
if (opts.transform) {
Object.assign(props.state, transformedState.value)
}
return props.state as T
}
@@ -170,8 +174,7 @@ async function onSubmitWrapper(payload: Event) {
const event = payload as FormSubmitEvent<any>
try {
await _validate({ nested: true })
event.data = props.schema ? transformedState.value : props.state
event.data = await _validate({ nested: true, transform: true })
await props.onSubmit?.(event)
} catch (error) {
if (!(error instanceof FormValidationException)) {