( form: FieldTree<TModel>, options?: FormSubmitOptions<unknown, TModel> | FormSubmitOptions<unknown, TModel>['action'], )
| 426 | action: NoInfer<FormSubmitOptions<unknown, TModel>['action']>, |
| 427 | ): Promise<boolean>; |
| 428 | export async function submit<TModel>( |
| 429 | form: FieldTree<TModel>, |
| 430 | options?: FormSubmitOptions<unknown, TModel> | FormSubmitOptions<unknown, TModel>['action'], |
| 431 | ): Promise<boolean> { |
| 432 | const node = untracked(form) as FieldState<unknown> as FieldNode; |
| 433 | |
| 434 | if (untracked(node.submitState.submitting)) { |
| 435 | return false; |
| 436 | } |
| 437 | |
| 438 | const field = options === undefined ? node.structure.root.fieldProxy : form; |
| 439 | const detail = {root: node.structure.root.fieldProxy, submitted: form}; |
| 440 | |
| 441 | // Normalize options. |
| 442 | options = |
| 443 | typeof options === 'function' |
| 444 | ? {action: options} |
| 445 | : (options ?? node.structure.fieldManager.submitOptions); |
| 446 | |
| 447 | // Verify that an action was provided. |
| 448 | const action = options?.action as FormSubmitOptions<unknown, unknown>['action']; |
| 449 | if (!action) { |
| 450 | throw new RuntimeError( |
| 451 | RuntimeErrorCode.MISSING_SUBMIT_ACTION, |
| 452 | (typeof ngDevMode === 'undefined' || ngDevMode) && |
| 453 | 'Cannot submit form with no submit action. Specify the action when creating the form, or as an additional argument to `submit()`.', |
| 454 | ); |
| 455 | } |
| 456 | |
| 457 | node.markAsTouched(); |
| 458 | |
| 459 | const onInvalid = options?.onInvalid as FormSubmitOptions<unknown, unknown>['onInvalid']; |
| 460 | const shouldRun = shouldRunAction(node, options?.ignoreValidators); |
| 461 | |
| 462 | // Run the action (or alternatively the `onInvalid` callback) |
| 463 | try { |
| 464 | if (shouldRun) { |
| 465 | node.submitState.selfSubmitting.set(true); |
| 466 | const errors = await untracked(() => action?.(field, detail)); |
| 467 | errors && setSubmissionErrors(node, errors); |
| 468 | return !errors || (isArray(errors) && errors.length === 0); |
| 469 | } else { |
| 470 | untracked(() => onInvalid?.(field, detail)); |
| 471 | } |
| 472 | return false; |
| 473 | } finally { |
| 474 | node.submitState.selfSubmitting.set(false); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Creates a `Schema` that adds logic rules to a form. |
no test coverage detected
searching dependent graphs…