MCPcopy Create free account
hub / github.com/angular/angular / submit

Function submit

packages/forms/signals/src/api/structure.ts:448–496  ·  view source on GitHub ↗
(
  form: FieldTree<TModel>,
  options?: FormSubmitOptions<unknown, TModel> | FormSubmitOptions<unknown, TModel>['action'],
)

Source from the content-addressed store, hash-verified

446 action: NoInfer<FormSubmitOptions<unknown, TModel>['action']>,
447): Promise<boolean>;
448export 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.

Callers 7

initWebMcpFormFunction · 0.90
onSubmitMethod · 0.90
onSubmitMethod · 0.90
onSubmitMethod · 0.90
onSubmitMethod · 0.90
submit.spec.tsFile · 0.85
compat.spec.tsFile · 0.85

Calls 6

untrackedFunction · 0.90
isArrayFunction · 0.90
shouldRunActionFunction · 0.85
setSubmissionErrorsFunction · 0.85
markAsTouchedMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected