/** * @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.io/license */ import {AfterViewInit, Directive, EventEmitter, forwardRef, Inject, Input, Optional, Self} from '@angular/core'; import {AbstractControl, FormControl, FormGroup, FormHooks} from '../model'; import {NG_ASYNC_VALIDATORS, NG_VALIDATORS} from '../validators'; import {ControlContainer} from './control_container'; import {Form} from './form_interface'; import {NgControl} from './ng_control'; import {NgModel} from './ng_model'; import {NgModelGroup} from './ng_model_group'; import {composeAsyncValidators, composeValidators, removeDir, setUpControl, setUpFormContainer, syncPendingControls} from './shared'; export const formDirectiveProvider: any = { provide: ControlContainer, useExisting: forwardRef(() => NgForm) }; const resolvedPromise = (() => Promise.resolve(null))(); /** * @description * Creates a top-level `FormGroup` instance and binds it to a form * to track aggregate form value and validation status. * * As soon as you import the `FormsModule`, this directive becomes active by default on * all `
* ``` * * ### Native DOM validation UI * * In order to prevent the native DOM form validation UI from interfering with Angular's form * validation, Angular automatically adds the `novalidate` attribute on any ` * ``` * * @ngModule FormsModule * @publicApi */ @Directive({ selector: 'form:not([ngNoForm]):not([formGroup]),ng-form,[ngForm]', providers: [formDirectiveProvider], host: {'(submit)': 'onSubmit($event)', '(reset)': 'onReset()'}, outputs: ['ngSubmit'], exportAs: 'ngForm' }) export class NgForm extends ControlContainer implements Form, AfterViewInit { /** * @description * Returns whether the form submission has been triggered. */ public readonly submitted: boolean = false; private _directives: NgModel[] = []; /** * @description * The `FormGroup` instance created for this form. */ form: FormGroup; /** * @description * Event emitter for the "ngSubmit" event */ ngSubmit = new EventEmitter(); /** * @description * Tracks options for the `NgForm` instance. * * **updateOn**: Sets the default `updateOn` value for all child `NgModels` below it * unless explicitly set by a child `NgModel` using `ngModelOptions`). Defaults to 'change'. * Possible values: `'change'` | `'blur'` | `'submit'`. * */ // TODO(issue/24571): remove '!'. @Input('ngFormOptions') options!: {updateOn?: FormHooks}; constructor( @Optional() @Self() @Inject(NG_VALIDATORS) validators: any[], @Optional() @Self() @Inject(NG_ASYNC_VALIDATORS) asyncValidators: any[]) { super(); this.form = new FormGroup({}, composeValidators(validators), composeAsyncValidators(asyncValidators)); } /** * @description * Lifecycle method called after the view is initialized. For internal use only. */ ngAfterViewInit() { this._setUpdateStrategy(); } /** * @description * The directive instance. */ get formDirective(): Form { return this; } /** * @description * The internal `FormGroup` instance. */ get control(): FormGroup { return this.form; } /** * @description * Returns an array representing the path to this group. Because this directive * always lives at the top level of a form, it is always an empty array. */ get path(): string[] { return []; } /** * @description * Returns a map of the controls in this group. */ get controls(): {[key: string]: AbstractControl} { return this.form.controls; } /** * @description * Method that sets up the control directive in this group, re-calculates its value * and validity, and adds the instance to the internal list of directives. * * @param dir The `NgModel` directive instance. */ addControl(dir: NgModel): void { resolvedPromise.then(() => { const container = this._findContainer(dir.path); (dir as {control: FormControl}).control =