MCPcopy
hub / github.com/angular/angular / submit

Function submit

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

Source from the content-addressed store, hash-verified

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

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

Used in the wild real call sites across dependent graphs

searching dependent graphs…