2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. 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
|
|
|
|
*/
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
import {Directive, Inject, Optional, Self, forwardRef} from '@angular/core';
|
|
|
|
|
2016-08-02 18:53:34 -04:00
|
|
|
import {EventEmitter} from '../facade/async';
|
2016-06-14 21:23:40 -04:00
|
|
|
import {ListWrapper} from '../facade/collection';
|
|
|
|
import {isPresent} from '../facade/lang';
|
2016-06-10 14:15:59 -04:00
|
|
|
import {AbstractControl, FormControl, FormGroup} from '../model';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {NG_ASYNC_VALIDATORS, NG_VALIDATORS} from '../validators';
|
|
|
|
|
|
|
|
import {ControlContainer} from './control_container';
|
2016-06-08 18:36:24 -04:00
|
|
|
import {Form} from './form_interface';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {NgControl} from './ng_control';
|
2016-06-15 18:15:41 -04:00
|
|
|
import {NgModel} from './ng_model';
|
2016-06-12 19:37:42 -04:00
|
|
|
import {NgModelGroup} from './ng_model_group';
|
2016-06-25 16:27:29 -04:00
|
|
|
import {composeAsyncValidators, composeValidators, setUpControl, setUpFormContainer} from './shared';
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-07-30 22:18:14 -04:00
|
|
|
export const formDirectiveProvider: any = {
|
|
|
|
provide: ControlContainer,
|
|
|
|
useExisting: forwardRef(() => NgForm)
|
|
|
|
};
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-08-02 18:53:34 -04:00
|
|
|
const resolvedPromise = Promise.resolve(null);
|
|
|
|
|
2016-06-08 18:36:24 -04:00
|
|
|
/**
|
2016-09-12 20:01:04 -04:00
|
|
|
* @whatItDoes Creates a top-level {@link FormGroup} instance and binds it to a form
|
|
|
|
* to track aggregate form value and validation status.
|
2016-06-08 18:36:24 -04:00
|
|
|
*
|
2016-09-12 20:01:04 -04:00
|
|
|
* @howToUse
|
2016-06-08 18:36:24 -04:00
|
|
|
*
|
2016-09-12 20:01:04 -04:00
|
|
|
* As soon as you import the `FormsModule`, this directive becomes active by default on
|
|
|
|
* all `<form>` tags. You don't need to add a special selector.
|
2016-06-08 18:36:24 -04:00
|
|
|
*
|
2016-09-12 20:01:04 -04:00
|
|
|
* You can export the directive into a local template variable using `ngForm` as the key
|
|
|
|
* (ex: `#myForm="ngForm"`). This is optional, but useful. Many properties from the underlying
|
|
|
|
* {@link FormGroup} instance are duplicated on the directive itself, so a reference to it
|
|
|
|
* will give you access to the aggregate value and validity status of the form, as well as
|
|
|
|
* user interaction properties like `dirty` and `touched`.
|
2016-06-08 18:36:24 -04:00
|
|
|
*
|
2016-09-12 20:01:04 -04:00
|
|
|
* To register child controls with the form, you'll want to use {@link NgModel} with a
|
|
|
|
* `name` attribute. You can also use {@link NgModelGroup} if you'd like to create
|
|
|
|
* sub-groups within the form.
|
2016-06-08 18:36:24 -04:00
|
|
|
*
|
2016-09-12 20:01:04 -04:00
|
|
|
* You can listen to the directive's `ngSubmit` event to be notified when the user has
|
2016-10-11 18:49:36 -04:00
|
|
|
* triggered a form submission. The `ngSubmit` event will be emitted with the original form
|
|
|
|
* submission event.
|
2016-06-08 18:36:24 -04:00
|
|
|
*
|
2016-09-12 20:01:04 -04:00
|
|
|
* {@example forms/ts/simpleForm/simple_form_example.ts region='Component'}
|
2016-06-08 18:36:24 -04:00
|
|
|
*
|
2016-09-12 20:01:04 -04:00
|
|
|
* * **npm package**: `@angular/forms`
|
2016-06-08 18:36:24 -04:00
|
|
|
*
|
2016-09-12 20:01:04 -04:00
|
|
|
* * **NgModule**: `FormsModule`
|
2016-06-08 18:36:24 -04:00
|
|
|
*
|
2016-08-17 10:44:39 -04:00
|
|
|
* @stable
|
2016-06-08 18:36:24 -04:00
|
|
|
*/
|
|
|
|
@Directive({
|
2016-06-10 22:10:17 -04:00
|
|
|
selector: 'form:not([ngNoForm]):not([formGroup]),ngForm,[ngForm]',
|
2016-06-08 18:36:24 -04:00
|
|
|
providers: [formDirectiveProvider],
|
2016-10-11 18:49:36 -04:00
|
|
|
host: {'(submit)': 'onSubmit($event)', '(reset)': 'onReset()'},
|
2016-06-08 18:36:24 -04:00
|
|
|
outputs: ['ngSubmit'],
|
|
|
|
exportAs: 'ngForm'
|
|
|
|
})
|
|
|
|
export class NgForm extends ControlContainer implements Form {
|
|
|
|
private _submitted: boolean = false;
|
|
|
|
|
2016-06-10 14:15:59 -04:00
|
|
|
form: FormGroup;
|
2016-06-08 18:36:24 -04:00
|
|
|
ngSubmit = new EventEmitter();
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
constructor(
|
|
|
|
@Optional() @Self() @Inject(NG_VALIDATORS) validators: any[],
|
|
|
|
@Optional() @Self() @Inject(NG_ASYNC_VALIDATORS) asyncValidators: any[]) {
|
2016-06-08 18:36:24 -04:00
|
|
|
super();
|
2016-08-24 19:58:43 -04:00
|
|
|
this.form =
|
|
|
|
new FormGroup({}, composeValidators(validators), composeAsyncValidators(asyncValidators));
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
get submitted(): boolean { return this._submitted; }
|
|
|
|
|
|
|
|
get formDirective(): Form { return this; }
|
|
|
|
|
2016-06-10 14:15:59 -04:00
|
|
|
get control(): FormGroup { return this.form; }
|
2016-06-08 18:36:24 -04:00
|
|
|
|
|
|
|
get path(): string[] { return []; }
|
|
|
|
|
|
|
|
get controls(): {[key: string]: AbstractControl} { return this.form.controls; }
|
|
|
|
|
2016-06-23 12:55:26 -04:00
|
|
|
addControl(dir: NgModel): void {
|
2016-08-02 18:53:34 -04:00
|
|
|
resolvedPromise.then(() => {
|
2016-06-10 13:09:50 -04:00
|
|
|
const container = this._findContainer(dir.path);
|
2016-06-23 12:55:26 -04:00
|
|
|
dir._control = <FormControl>container.registerControl(dir.name, dir.control);
|
2016-06-15 18:15:41 -04:00
|
|
|
setUpControl(dir.control, dir);
|
|
|
|
dir.control.updateValueAndValidity({emitEvent: false});
|
2016-06-08 18:36:24 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-05 16:35:17 -04:00
|
|
|
getControl(dir: NgModel): FormControl { return <FormControl>this.form.get(dir.path); }
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-15 18:15:41 -04:00
|
|
|
removeControl(dir: NgModel): void {
|
2016-08-02 18:53:34 -04:00
|
|
|
resolvedPromise.then(() => {
|
2016-10-11 18:49:36 -04:00
|
|
|
const container = this._findContainer(dir.path);
|
2016-06-08 18:36:24 -04:00
|
|
|
if (isPresent(container)) {
|
|
|
|
container.removeControl(dir.name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-12 19:37:42 -04:00
|
|
|
addFormGroup(dir: NgModelGroup): void {
|
2016-08-02 18:53:34 -04:00
|
|
|
resolvedPromise.then(() => {
|
2016-10-11 18:49:36 -04:00
|
|
|
const container = this._findContainer(dir.path);
|
|
|
|
const group = new FormGroup({});
|
2016-06-25 16:27:29 -04:00
|
|
|
setUpFormContainer(group, dir);
|
2016-06-08 18:36:24 -04:00
|
|
|
container.registerControl(dir.name, group);
|
|
|
|
group.updateValueAndValidity({emitEvent: false});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-12 19:37:42 -04:00
|
|
|
removeFormGroup(dir: NgModelGroup): void {
|
2016-08-02 18:53:34 -04:00
|
|
|
resolvedPromise.then(() => {
|
2016-10-11 18:49:36 -04:00
|
|
|
const container = this._findContainer(dir.path);
|
2016-06-08 18:36:24 -04:00
|
|
|
if (isPresent(container)) {
|
|
|
|
container.removeControl(dir.name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-05 16:35:17 -04:00
|
|
|
getFormGroup(dir: NgModelGroup): FormGroup { return <FormGroup>this.form.get(dir.path); }
|
2016-06-08 18:36:24 -04:00
|
|
|
|
|
|
|
updateModel(dir: NgControl, value: any): void {
|
2016-08-02 18:53:34 -04:00
|
|
|
resolvedPromise.then(() => {
|
2016-10-11 18:49:36 -04:00
|
|
|
const ctrl = <FormControl>this.form.get(dir.path);
|
2016-08-05 16:35:17 -04:00
|
|
|
ctrl.setValue(value);
|
2016-06-08 18:36:24 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-05 16:35:17 -04:00
|
|
|
setValue(value: {[key: string]: any}): void { this.control.setValue(value); }
|
2016-07-08 16:04:25 -04:00
|
|
|
|
2016-10-11 18:49:36 -04:00
|
|
|
onSubmit($event: Event): boolean {
|
2016-06-08 18:36:24 -04:00
|
|
|
this._submitted = true;
|
2016-10-11 18:49:36 -04:00
|
|
|
this.ngSubmit.emit($event);
|
2016-06-08 18:36:24 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-12 02:27:33 -04:00
|
|
|
onReset(): void { this.resetForm(); }
|
|
|
|
|
|
|
|
resetForm(value: any = undefined): void {
|
|
|
|
this.form.reset(value);
|
|
|
|
this._submitted = false;
|
|
|
|
}
|
2016-07-12 18:02:25 -04:00
|
|
|
|
2016-06-08 18:36:24 -04:00
|
|
|
/** @internal */
|
2016-06-10 14:15:59 -04:00
|
|
|
_findContainer(path: string[]): FormGroup {
|
2016-06-08 18:36:24 -04:00
|
|
|
path.pop();
|
2016-08-05 16:35:17 -04:00
|
|
|
return ListWrapper.isEmpty(path) ? this.form : <FormGroup>this.form.get(path);
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
}
|