MCPcopy Create free account
hub / github.com/react-hook-form/react-hook-form / handleSubmit

Function handleSubmit

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

Source from the content-addressed store, hash-verified

1393
1394 const handleSubmit: UseFormHandleSubmit<TFieldValues, TTransformedValues> =
1395 (onValid, onInvalid) => async (e) => {
1396 let onValidError = undefined;
1397 if (e) {
1398 e.preventDefault && e.preventDefault();
1399 (e as React.BaseSyntheticEvent).persist &&
1400 (e as React.BaseSyntheticEvent).persist();
1401 }
1402 let fieldValues:
1403 | TFieldValues
1404 | TTransformedValues
1405 | Record<string, never> = cloneObject(_formValues);
1406
1407 _subjects.state.next({
1408 isSubmitting: true,
1409 });
1410
1411 if (_options.resolver) {
1412 const { errors, values } = await _runSchema();
1413 _updateIsValidating();
1414 _formState.errors = errors;
1415 fieldValues = cloneObject(values);
1416 } else {
1417 await executeBuiltInValidation({
1418 fields: _fields,
1419 eventType: EVENTS.SUBMIT,
1420 });
1421 }
1422
1423 if (_names.disabled.size) {
1424 for (const name of _names.disabled) {
1425 unset(fieldValues, name);
1426 }
1427 }
1428
1429 unset(_formState.errors, ROOT_ERROR_TYPE);
1430
1431 if (isEmptyObject(_formState.errors)) {
1432 _subjects.state.next({
1433 errors: {},
1434 });
1435 try {
1436 await onValid(fieldValues as TTransformedValues, e);
1437 } catch (error) {
1438 onValidError = error;
1439 }
1440 } else {
1441 if (onInvalid) {
1442 await onInvalid({ ..._formState.errors }, e);
1443 }
1444 _focusError();
1445 setTimeout(_focusError);
1446 }
1447
1448 _subjects.state.next({
1449 isSubmitted: true,
1450 isSubmitting: false,
1451 isSubmitSuccessful: isEmptyObject(_formState.errors) && !onValidError,
1452 submitCount: _formState.submitCount + 1,

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