fix(@vben-core/form-ui): harden codec value boundaries

This commit is contained in:
Dream
2026-07-23 15:33:57 +08:00
parent 1ac5ac3e82
commit decbed20bc
4 changed files with 89 additions and 6 deletions

View File

@@ -144,7 +144,7 @@ export class FormApi<
if (this.state?.codec) {
return encodeFormValues(
this.state.codec,
rawValues as Readonly<TFormValues>,
cloneDeep(toRaw(rawValues)) as Readonly<TFormValues>,
);
}
return formatFormValues(
@@ -303,10 +303,23 @@ export class FormApi<
if (!this.isMounted) {
this.form = formActions;
this.stateHandler.setConditionTrue();
const initialValues = this.form.values
? this.formatValues(toRaw(this.form.values))
: {};
this.setLatestSubmissionValues(initialValues);
let initialValues: FormValues = {};
if (this.form.values) {
const rawInitialValues = toRaw(this.form.values);
try {
initialValues = this.formatValues(rawInitialValues);
} catch (error) {
if (!this.state?.codec) {
throw error;
}
console.warn(
'[Vben Form] Failed to encode initial values. Falling back to raw form values.',
error,
);
initialValues = cloneDeep(rawInitialValues);
}
}
this.setLatestSubmissionValues(initialValues as Partial<TSubmitValues>);
this.componentRefMap =
componentRefMap ?? this.componentRefMap ?? new Map();
this.isMounted = true;

View File

@@ -678,7 +678,7 @@ export type HandleSubmitFn<
TFormValues extends FormValues = FormValues,
TSubmitValues extends FormValues = TFormValues,
> = (
values: TSubmitValues,
values: NoInfer<TSubmitValues>,
rawValues: Readonly<TFormValues>,
) => Promise<void> | void;