Skip to content

Commit 2c69266

Browse files
committed
Factorized client validation result in enhance
1 parent 8e297d0 commit 2c69266

File tree

1 file changed

+35
-35
lines changed

1 file changed

+35
-35
lines changed

src/lib/client/superForm.ts

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,17 +1604,39 @@ export function superForm<
16041604

16051605
const _submitCancel = submit.cancel;
16061606
let cancelled = false;
1607-
function cancel(resetTimers = true) {
1607+
1608+
function clientValidationResult(validation: SuperFormValidated<T, M, In>) {
1609+
const validationResult = { ...validation, posted: true };
1610+
1611+
const status = validationResult.valid
1612+
? 200
1613+
: (typeof options.SPA === 'boolean' ? undefined : options.SPA?.failStatus) ?? 400;
1614+
1615+
const data = { form: validationResult };
1616+
1617+
const result: ActionResult = validationResult.valid
1618+
? { type: 'success', status, data }
1619+
: { type: 'failure', status, data };
1620+
1621+
setTimeout(() => validationResponse({ result }, cancelled), 0);
1622+
}
1623+
1624+
function cancel(
1625+
opts: { resetTimers?: boolean } = {
1626+
resetTimers: true
1627+
}
1628+
) {
16081629
cancelled = true;
1609-
if (resetTimers && htmlForm.isSubmitting()) {
1630+
1631+
if (opts.resetTimers && htmlForm.isSubmitting()) {
16101632
htmlForm.completed({ cancelled });
16111633
}
16121634
return _submitCancel();
16131635
}
16141636
submit.cancel = cancel;
16151637

16161638
if (htmlForm.isSubmitting() && options.multipleSubmits == 'prevent') {
1617-
cancel(false);
1639+
cancel({ resetTimers: false });
16181640
} else {
16191641
if (htmlForm.isSubmitting() && options.multipleSubmits == 'abort') {
16201642
if (currentRequest) currentRequest.abort();
@@ -1627,9 +1649,9 @@ export function superForm<
16271649
}
16281650
}
16291651

1630-
if (cancelled) {
1631-
if (options.flashMessage) cancelFlash(options);
1632-
} else {
1652+
if (cancelled && options.flashMessage) cancelFlash(options);
1653+
1654+
if (!cancelled) {
16331655
// Client validation
16341656
const noValidate =
16351657
!options.SPA &&
@@ -1648,16 +1670,8 @@ export function superForm<
16481670
validation = await validateForm();
16491671

16501672
if (!validation.valid) {
1651-
cancel(false);
1652-
1653-
const result = {
1654-
type: 'failure' as const,
1655-
status:
1656-
(typeof options.SPA === 'boolean' ? undefined : options.SPA?.failStatus) ?? 400,
1657-
data: { form: validation }
1658-
};
1659-
1660-
setTimeout(() => validationResponse({ result }), 0);
1673+
cancel({ resetTimers: false });
1674+
clientValidationResult(validation);
16611675
}
16621676
}
16631677

@@ -1695,22 +1709,9 @@ export function superForm<
16951709
lastInputChange = undefined;
16961710

16971711
if (options.SPA) {
1698-
cancel(false);
16991712
if (!validation) validation = await validateForm();
1700-
1701-
const validationResult = { ...validation, posted: true };
1702-
1703-
const result = {
1704-
type: validationResult.valid ? 'success' : 'failure',
1705-
status: validationResult.valid
1706-
? 200
1707-
: typeof options.SPA == 'object'
1708-
? options.SPA?.failStatus
1709-
: 400 ?? 400,
1710-
data: { form: validationResult }
1711-
} as ActionResult;
1712-
1713-
setTimeout(() => validationResponse({ result }), 0);
1713+
cancel({ resetTimers: false });
1714+
clientValidationResult(validation);
17141715
} else if (options.dataType === 'json') {
17151716
if (!validation) validation = await validateForm();
17161717

@@ -1772,7 +1773,9 @@ export function superForm<
17721773
return chunks;
17731774
}
17741775

1775-
async function validationResponse(event: ValidationResponse) {
1776+
async function validationResponse(event: ValidationResponse, cancelled = false) {
1777+
currentRequest = null;
1778+
17761779
// Check if an error was thrown in hooks, in which case it has no type.
17771780
const result: ActionResult = event.result.type
17781781
? event.result
@@ -1782,9 +1785,6 @@ export function superForm<
17821785
error: event.result
17831786
};
17841787

1785-
currentRequest = null;
1786-
1787-
let cancelled = false;
17881788
const cancel = () => (cancelled = true);
17891789

17901790
const data = {

0 commit comments

Comments
 (0)