/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.dev/license */ import {Directive, input, untracked} from '@angular/core'; import {submit} from '../api/structure'; import {FieldState, FieldTree} from '../api/types'; import {FieldNode} from '../field/node'; /** * A directive that binds a `FieldTree` to a `
` element. * * It automatically: * 1. Sets `novalidate` on the form element to disable browser validation. * 2. Listens for the `submit` event, prevents the default behavior, and calls `submit()` on the * `FieldTree` if it defines its own submission options. * * @usageNotes * * ```html * * ... *
* ``` * * @see [Setting up form submission with FormRoot](guide/forms/signals/form-submission#setting-up-form-submission-with-formroot) * * @publicApi 22.0 */ @Directive({ selector: 'form[formRoot]', host: { 'novalidate': '', '(submit)': 'onSubmit($event)', }, }) export class FormRoot { readonly fieldTree = input.required>({alias: 'formRoot'}); protected onSubmit(event: Event): void { event.preventDefault(); untracked(() => { const fieldTree = this.fieldTree(); const node = fieldTree() as FieldState as FieldNode; if (node.structure.fieldManager.submitOptions) { submit(fieldTree); } }); } }