MCPcopy
hub / github.com/react-hook-form/react-hook-form / handleSubmit

Function handleSubmit

src/logic/createFormControl.ts:1574–1638  ·  view source on GitHub ↗
(onValid, onInvalid)

Source from the content-addressed store, hash-verified

1572
1573 const handleSubmit: UseFormHandleSubmit<TFieldValues, TTransformedValues> =
1574 (onValid, onInvalid) => async (e) => {
1575 let onValidError = undefined;
1576 if (e) {
1577 e.preventDefault && e.preventDefault();
1578 (e as React.BaseSyntheticEvent).persist &&
1579 (e as React.BaseSyntheticEvent).persist();
1580 }
1581 let fieldValues:
1582 | TFieldValues
1583 | TTransformedValues
1584 | Record<string, never> = cloneObject(_formValues);
1585
1586 _subjects.state.next({
1587 isSubmitting: true,
1588 });
1589
1590 if (_options.resolver) {
1591 const { errors, values } = await _runSchema();
1592 _updateIsValidating();
1593 _formState.errors = errors;
1594 fieldValues = cloneObject(values);
1595 } else {
1596 await executeBuiltInValidation({
1597 fields: _fields,
1598 eventType: EVENTS.SUBMIT,
1599 });
1600 }
1601
1602 if (_names.disabled.size) {
1603 for (const name of _names.disabled) {
1604 unset(fieldValues, name);
1605 }
1606 }
1607
1608 unset(_formState.errors, ROOT_ERROR_TYPE);
1609
1610 if (isEmptyObject(_formState.errors)) {
1611 _subjects.state.next({
1612 errors: {},
1613 });
1614 try {
1615 await onValid(fieldValues as TTransformedValues, e);
1616 } catch (error) {
1617 onValidError = error;
1618 }
1619 } else {
1620 if (onInvalid) {
1621 await onInvalid({ ..._formState.errors }, e);
1622 }
1623
1624 _focusError();
1625 setTimeout(_focusError);
1626 }
1627
1628 _subjects.state.next({
1629 isSubmitted: true,
1630 isSubmitting: false,
1631 isSubmitSuccessful: isEmptyObject(_formState.errors) && !onValidError,

Callers 15

form.test-d.tsFile · 0.85
ComponentFunction · 0.85
AppFunction · 0.85
TestComponentFunction · 0.85
ComponentFunction · 0.85
ParentFunction · 0.85
AppFunction · 0.85
ComponentFunction · 0.85
AppFunction · 0.85
AppFunction · 0.85
ChildFunction · 0.85

Calls 8

cloneObjectFunction · 0.85
_runSchemaFunction · 0.85
_updateIsValidatingFunction · 0.85
executeBuiltInValidationFunction · 0.85
unsetFunction · 0.85
onValidFunction · 0.85
onInvalidFunction · 0.85
_focusErrorFunction · 0.85

Tested by 15

ComponentFunction · 0.68
AppFunction · 0.68
TestComponentFunction · 0.68
ComponentFunction · 0.68
ParentFunction · 0.68
AppFunction · 0.68
ComponentFunction · 0.68
AppFunction · 0.68
AppFunction · 0.68
ChildFunction · 0.68
AppFunction · 0.68
ComponentFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…