( form: FieldTree<TModel>, options?: FormSubmitOptions<unknown, TModel> | FormSubmitOptions<unknown, TModel>['action'], )
| 461 | action: NoInfer<FormSubmitOptions<unknown, TModel>['action']>, |
| 462 | ): Promise<boolean>; |
| 463 | export async function submit<TModel>( |
| 464 | form: FieldTree<TModel>, |
| 465 | options?: FormSubmitOptions<unknown, TModel> | FormSubmitOptions<unknown, TModel>['action'], |
| 466 | ): Promise<boolean> { |
| 467 | const node = untracked(form) as FieldState<unknown> as FieldNode; |
| 468 | |
| 469 | if (untracked(node.submitState.submitting)) { |
| 470 | return false; |
| 471 | } |
| 472 | |
| 473 | const field = options === undefined ? node.structure.root.fieldProxy : form; |
| 474 | const detail = {root: node.structure.root.fieldProxy, submitted: form}; |
| 475 | |
| 476 | // Normalize options. |
| 477 | options = |
| 478 | typeof options === 'function' |
| 479 | ? {action: options} |
| 480 | : (options ?? node.structure.fieldManager.submitOptions); |
| 481 | |
| 482 | // Verify that an action was provided. |
| 483 | const action = options?.action as FormSubmitOptions<unknown, unknown>['action']; |
| 484 | if (!action) { |
| 485 | throw new RuntimeError( |
| 486 | RuntimeErrorCode.MISSING_SUBMIT_ACTION, |
| 487 | (typeof ngDevMode === 'undefined' || ngDevMode) && |
| 488 | 'Cannot submit form with no submit action. Specify the action when creating the form, or as an additional argument to `submit()`.', |
| 489 | ); |
| 490 | } |
| 491 | |
| 492 | node.markAsTouched(); |
| 493 | |
| 494 | const onInvalid = options?.onInvalid as FormSubmitOptions<unknown, unknown>['onInvalid']; |
| 495 | const shouldRun = shouldRunAction(node, options?.ignoreValidators); |
| 496 | |
| 497 | // Run the action (or alternatively the `onInvalid` callback) |
| 498 | try { |
| 499 | if (shouldRun) { |
| 500 | node.submitState.selfSubmitting.set(true); |
| 501 | const errors = await untracked(() => action?.(field, detail)); |
| 502 | errors && setSubmissionErrors(node, errors); |
| 503 | return !errors || (isArray(errors) && errors.length === 0); |
| 504 | } else { |
| 505 | untracked(() => onInvalid?.(field, detail)); |
| 506 | } |
| 507 | return false; |
| 508 | } finally { |
| 509 | node.submitState.selfSubmitting.set(false); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Creates a `Schema` that adds logic rules to a form. |
no test coverage detected