Skip to content

Commit 54a8273

Browse files
committed
Added focusOnError option to validateForm
1 parent 0bd91e8 commit 54a8273

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- [VineJS](https://vinejs.dev/) adapter!
13+
- Added `focusOnError` option to `validateForm`, default `true`.
1314

1415
### Fixed
1516

src/lib/client/superForm.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -736,8 +736,6 @@ export function superForm<
736736
}
737737
}
738738

739-
let addedError = false;
740-
741739
traversePaths(errors, (error) => {
742740
if (!Array.isArray(error.value)) return;
743741

@@ -748,7 +746,6 @@ export function superForm<
748746

749747
function addError() {
750748
//console.log('Adding error', `[${error.path.join('.')}]`, error.value); //debug
751-
addedError = true;
752749
setPaths(output, [error.path], error.value);
753750

754751
if (options.customValidity && isEventError && validity.has(joinedPath)) {
@@ -821,13 +818,6 @@ export function superForm<
821818
});
822819

823820
Errors.set(output as ValidationErrors<T>);
824-
825-
if (addedError && EnhancedForm) {
826-
// Focus on first error field
827-
setTimeout(() => {
828-
if (EnhancedForm) scrollToFirstError(EnhancedForm, options);
829-
}, 1);
830-
}
831821
}
832822

833823
function Form_set(data: T, options: FormDataOptions = {}) {
@@ -1366,7 +1356,11 @@ export function superForm<
13661356
},
13671357

13681358
async validateForm<P extends Partial<T> = T>(
1369-
opts: { update?: boolean; schema?: ValidationAdapter<P> } = {}
1359+
opts: {
1360+
update?: boolean;
1361+
schema?: ValidationAdapter<P>;
1362+
focusOnError?: boolean;
1363+
} = {}
13701364
): Promise<SuperFormValidated<T, M, In>> {
13711365
if (!options.validators && !opts.schema) {
13721366
throw new SuperFormError(
@@ -1378,6 +1372,17 @@ export function superForm<
13781372
? await Form_clientValidation({ paths: [] }, true, opts.schema)
13791373
: Form_validate({ adapter: opts.schema });
13801374

1375+
if (opts.update && EnhancedForm) {
1376+
// Focus on first error field
1377+
setTimeout(() => {
1378+
if (EnhancedForm)
1379+
scrollToFirstError(EnhancedForm, {
1380+
...options,
1381+
scrollToError: opts.focusOnError === false ? 'off' : options.scrollToError
1382+
});
1383+
}, 1);
1384+
}
1385+
13811386
return result || Form_validate({ adapter: opts.schema });
13821387
},
13831388

0 commit comments

Comments
 (0)