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