@@ -521,13 +521,13 @@ export function superForm<
521
521
onDestroy ( ( ) => {
522
522
Unsubscriptions_unsubscribe ( ) ;
523
523
NextChange_clear ( ) ;
524
+ EnhancedForm_destroy ( ) ;
524
525
525
526
for ( const events of Object . values ( formEvents ) ) {
526
527
events . length = 0 ;
527
528
}
528
529
529
530
formIds . get ( _currentPage ) ?. delete ( _initialFormId ) ;
530
- ActionForm_remove ( ) ;
531
531
} ) ;
532
532
533
533
// Check for nested objects, throw if datatype isn't json
@@ -770,7 +770,7 @@ export function superForm<
770
770
771
771
if ( skipValidation || ! event || ! options . validators || options . validators == 'clear' ) {
772
772
if ( event ?. paths ) {
773
- const formElement = event ?. formElement ?? EnhancedForm ;
773
+ const formElement = event ?. formElement ?? EnhancedForm_get ( ) ;
774
774
if ( formElement ) Form__clearCustomValidity ( formElement , event . paths ) ;
775
775
}
776
776
return ;
@@ -820,7 +820,7 @@ export function superForm<
820
820
const output : Record < string , unknown > = { } ;
821
821
let validity = new Map < string , { el : HTMLElement ; message : string } > ( ) ;
822
822
823
- const formElement = event . formElement ?? EnhancedForm ;
823
+ const formElement = event . formElement ?? EnhancedForm_get ( ) ;
824
824
if ( formElement ) validity = Form__clearCustomValidity ( formElement , event . paths ) ;
825
825
826
826
traversePaths ( errors , ( error ) => {
@@ -1246,28 +1246,34 @@ export function superForm<
1246
1246
1247
1247
//#endregion
1248
1248
1249
- //#region ActionForm
1249
+ //#region EnhancedForm
1250
1250
1251
- // SPA action mode
1252
- let ActionForm : HTMLFormElement | undefined = undefined ;
1251
+ /**
1252
+ * Used for SPA action mode and options.customValidity to display errors, even if programmatically set
1253
+ */
1254
+ let EnhancedForm : HTMLFormElement | undefined ;
1253
1255
1254
- function ActionForm_create ( action : string ) {
1255
- ActionForm = document . createElement ( 'form' ) ;
1256
- ActionForm . method = 'POST' ;
1257
- ActionForm . action = action ;
1258
- superFormEnhance ( ActionForm ) ;
1259
- document . body . appendChild ( ActionForm ) ;
1256
+ function EnhancedForm_get ( ) {
1257
+ return EnhancedForm ;
1260
1258
}
1261
1259
1262
- function ActionForm_setAction ( action : string ) {
1263
- if ( ActionForm ) ActionForm . action = action ;
1260
+ function EnhancedForm_createFromSPA ( action : string ) {
1261
+ EnhancedForm = document . createElement ( 'form' ) ;
1262
+ EnhancedForm . method = 'POST' ;
1263
+ EnhancedForm . action = action ;
1264
+ superFormEnhance ( EnhancedForm ) ;
1265
+ document . body . appendChild ( EnhancedForm ) ;
1264
1266
}
1265
1267
1266
- function ActionForm_remove ( ) {
1267
- if ( ActionForm ?. parentElement ) {
1268
- ActionForm . remove ( ) ;
1269
- ActionForm = undefined ;
1268
+ function EnhancedForm_setAction ( action : string ) {
1269
+ if ( EnhancedForm ) EnhancedForm . action = action ;
1270
+ }
1271
+
1272
+ function EnhancedForm_destroy ( ) {
1273
+ if ( EnhancedForm ?. parentElement ) {
1274
+ EnhancedForm . remove ( ) ;
1270
1275
}
1276
+ EnhancedForm = undefined ;
1271
1277
}
1272
1278
1273
1279
//#endregion
@@ -1277,9 +1283,6 @@ export function superForm<
1277
1283
( $errors : ValidationErrors < T > | undefined ) => ( $errors ? flattenErrors ( $errors ) : [ ] )
1278
1284
) ;
1279
1285
1280
- // Used for options.customValidity to display errors, even if programmatically set
1281
- let EnhancedForm : HTMLFormElement | undefined ;
1282
-
1283
1286
///// End of Roles //////////////////////////////////////////////////////////
1284
1287
1285
1288
// Need to clear this and set it again when use:enhance has run, to avoid showing the
@@ -1438,7 +1441,7 @@ export function superForm<
1438
1441
) ;
1439
1442
1440
1443
if ( typeof options . SPA === 'string' ) {
1441
- ActionForm_create ( options . SPA ) ;
1444
+ EnhancedForm_createFromSPA ( options . SPA ) ;
1442
1445
}
1443
1446
}
1444
1447
@@ -1449,8 +1452,15 @@ export function superForm<
1449
1452
* @DCI -context
1450
1453
*/
1451
1454
function superFormEnhance ( FormElement : HTMLFormElement , events ?: SuperFormEvents < T , M > ) {
1452
- ActionForm_remove ( ) ;
1453
- EnhancedForm = FormElement ;
1455
+ if ( FormElement . method == 'get' ) FormElement . method = 'post' ;
1456
+
1457
+ if ( typeof options . SPA === 'string' ) {
1458
+ if ( options . SPA . length && FormElement . action == document . location . href ) {
1459
+ FormElement . action = options . SPA ;
1460
+ }
1461
+ } else {
1462
+ EnhancedForm = FormElement ;
1463
+ }
1454
1464
1455
1465
if ( events ) {
1456
1466
if ( events . onError ) {
@@ -1523,7 +1533,6 @@ export function superForm<
1523
1533
onDestroy ( ( ) => {
1524
1534
FormElement . removeEventListener ( 'focusout' , onBlur ) ;
1525
1535
FormElement . removeEventListener ( 'input' , onInput ) ;
1526
- EnhancedForm = undefined ;
1527
1536
} ) ;
1528
1537
1529
1538
///// SvelteKit enhance function //////////////////////////////////
@@ -1716,7 +1725,7 @@ export function superForm<
1716
1725
}
1717
1726
1718
1727
if ( typeof options . SPA === 'string' ) {
1719
- ActionForm_setAction ( options . SPA ) ;
1728
+ EnhancedForm_setAction ( options . SPA ) ;
1720
1729
}
1721
1730
}
1722
1731
}
@@ -2045,14 +2054,15 @@ export function superForm<
2045
2054
)
2046
2055
: Form_validate ( { adapter : opts . schema } ) ;
2047
2056
2048
- if ( opts . update && EnhancedForm ) {
2057
+ const enhancedForm = EnhancedForm_get ( ) ;
2058
+ if ( opts . update && enhancedForm ) {
2049
2059
// Focus on first error field
2050
2060
setTimeout ( ( ) => {
2051
- if ( EnhancedForm )
2052
- scrollToFirstError ( EnhancedForm , {
2053
- ...options ,
2054
- scrollToError : opts . focusOnError === false ? 'off' : options . scrollToError
2055
- } ) ;
2061
+ if ( ! enhancedForm ) return ;
2062
+ scrollToFirstError ( enhancedForm , {
2063
+ ...options ,
2064
+ scrollToError : opts . focusOnError === false ? 'off' : options . scrollToError
2065
+ } ) ;
2056
2066
} , 1 ) ;
2057
2067
}
2058
2068
@@ -2072,8 +2082,8 @@ export function superForm<
2072
2082
} ,
2073
2083
2074
2084
submit ( submitter ?: HTMLElement | Event | EventTarget | null | undefined ) {
2075
- const form = EnhancedForm
2076
- ? EnhancedForm
2085
+ const form = EnhancedForm_get ( )
2086
+ ? EnhancedForm_get ( )
2077
2087
: submitter && submitter instanceof HTMLElement
2078
2088
? submitter . closest < HTMLFormElement > ( 'form' )
2079
2089
: undefined ;
0 commit comments