@@ -255,9 +255,10 @@ export type SuperForm<T extends ZodValidation<AnyZodObject>, M = any> = {
255255export type EnhancedForm < T extends AnyZodObject , M = any > = SuperForm < T , M > ;
256256
257257const formIds = new WeakMap < Page , Set < string | undefined > > ( ) ;
258-
259- // Track ActionData, so it won't be applied twice for componentized forms.
260- const postedActionData = new WeakSet < object > ( ) ;
258+ const initializedForms = new WeakMap <
259+ object ,
260+ SuperValidated < ZodValidation < AnyZodObject > , unknown >
261+ > ( ) ;
261262
262263function multipleFormIdError ( id : string | undefined ) {
263264 return (
@@ -341,8 +342,22 @@ export function superForm<
341342 }
342343 }
343344
344- // Need to clone the validation data, in case it's used to populate multiple forms.
345- const initialForm = clone ( form ) ;
345+ // Need to clone the form data, in case it's used to populate multiple forms and in components
346+ // that are mounted and destroyed multiple times.
347+ if ( ! initializedForms . has ( form ) ) {
348+ initializedForms . set ( form , clone ( form ) ) ;
349+ }
350+ const initialForm = initializedForms . get ( form ) as SuperValidated < T , M > ;
351+
352+ if ( typeof initialForm . valid !== 'boolean' ) {
353+ throw new SuperFormError (
354+ 'A non-validation object was passed to superForm. ' +
355+ 'It should be an object of type SuperValidated, usually returned from superValidate.'
356+ ) ;
357+ }
358+
359+ // Restore the initial data, in case of a mounted/destroyed component.
360+ let _initiallyCloned = false ;
346361
347362 // Detect if a form is posted without JavaScript.
348363 const postedData = _currentPage . form ;
@@ -351,9 +366,9 @@ export function superForm<
351366 for ( const postedForm of Context_findValidationForms (
352367 postedData
353368 ) . reverse ( ) ) {
354- if ( postedForm . id === _formId && ! postedActionData . has ( postedForm ) ) {
369+ if ( postedForm . id === _formId && ! initializedForms . has ( postedForm ) ) {
355370 // Prevent multiple "posting" that can happen when components are recreated.
356- postedActionData . add ( postedData ) ;
371+ initializedForms . set ( postedData , postedData ) ;
357372
358373 const pageDataForm = form as SuperValidated < T , M > ;
359374 form = postedForm as SuperValidated < T , M > ;
@@ -364,21 +379,17 @@ export function superForm<
364379 ( options . resetForm === true || options . resetForm ( ) )
365380 ) {
366381 form = clone ( pageDataForm ) ;
367- form . message = postedForm . message ;
382+ form . message = clone ( postedForm . message ) ;
383+ _initiallyCloned = true ;
368384 }
369385 break ;
370386 }
371387 }
372388 }
373389
374- const form2 = form as SuperValidated < T , M > ;
390+ if ( ! _initiallyCloned ) form = clone ( initialForm ) ;
375391
376- if ( typeof initialForm . valid !== 'boolean' ) {
377- throw new SuperFormError (
378- 'A non-validation object was passed to superForm. ' +
379- 'It should be an object of type SuperValidated, usually returned from superValidate.'
380- ) ;
381- }
392+ const form2 = form as SuperValidated < T , M > ;
382393
383394 // Underlying store for Errors
384395 const _errors = writable ( form2 . errors ) ;
@@ -833,12 +844,12 @@ export function superForm<
833844 const forms = Context_findValidationForms ( actionData ) ;
834845 for ( const newForm of forms ) {
835846 //console.log('🚀~ ActionData ~ newForm:', newForm.id);
836- if ( newForm . id !== _formId || postedActionData . has ( newForm ) ) {
847+ if ( newForm . id !== _formId || initializedForms . has ( newForm ) ) {
837848 continue ;
838849 }
839850
840851 // Prevent multiple "posting" that can happen when components are recreated.
841- postedActionData . add ( newForm ) ;
852+ initializedForms . set ( newForm , newForm ) ;
842853
843854 await Form_updateFromValidation (
844855 newForm as SuperValidated < T , M > ,
@@ -851,7 +862,9 @@ export function superForm<
851862 const forms = Context_findValidationForms ( pageUpdate . data ) ;
852863 for ( const newForm of forms ) {
853864 //console.log('🚀 ~ PageData ~ newForm:', newForm.id);
854- if ( newForm . id !== _formId ) continue ;
865+ if ( newForm . id !== _formId || initializedForms . has ( newForm ) ) {
866+ continue ;
867+ }
855868
856869 rebind ( newForm as SuperValidated < T , M > , untaint ) ;
857870 }
0 commit comments