diff --git a/modules/@angular/examples/forms/ts/nestedFormGroup/e2e_test/nested_form_group_spec.ts b/modules/@angular/examples/forms/ts/nestedFormGroup/e2e_test/nested_form_group_spec.ts new file mode 100644 index 0000000000..a879c1a926 --- /dev/null +++ b/modules/@angular/examples/forms/ts/nestedFormGroup/e2e_test/nested_form_group_spec.ts @@ -0,0 +1,43 @@ +/** + * @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 + */ + +import {verifyNoBrowserErrors} from '../../../../_common/e2e_util'; + +describe('nestedFormGroup example', () => { + afterEach(verifyNoBrowserErrors); + let firstInput: ElementFinder; + let lastInput: ElementFinder; + let button: ElementFinder; + + beforeEach(() => { + browser.get('/forms/ts/nestedFormGroup/index.html'); + firstInput = element(by.css('[formControlName="first"]')); + lastInput = element(by.css('[formControlName="last"]')); + button = element(by.css('button:not([type="submit"])')); + }); + + it('should populate the UI with initial values', () => { + expect(firstInput.getAttribute('value')).toEqual('Nancy'); + expect(lastInput.getAttribute('value')).toEqual('Drew'); + }); + + it('should show the error when name is invalid', () => { + firstInput.click(); + firstInput.clear(); + firstInput.sendKeys('a'); + + expect(element(by.css('p')).getText()).toEqual('Name is invalid.'); + }); + + it('should set the value programmatically', () => { + button.click(); + expect(firstInput.getAttribute('value')).toEqual('Bess'); + expect(lastInput.getAttribute('value')).toEqual('Marvin'); + }); + +}); diff --git a/modules/@angular/examples/forms/ts/nestedFormGroup/module.ts b/modules/@angular/examples/forms/ts/nestedFormGroup/module.ts new file mode 100644 index 0000000000..5773d5281f --- /dev/null +++ b/modules/@angular/examples/forms/ts/nestedFormGroup/module.ts @@ -0,0 +1,20 @@ +/** + * @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 + */ + +import {NgModule} from '@angular/core'; +import {ReactiveFormsModule} from '@angular/forms'; +import {BrowserModule} from '@angular/platform-browser'; +import {NestedFormGroupComp} from './nested_form_group_example'; + +@NgModule({ + imports: [BrowserModule, ReactiveFormsModule], + declarations: [NestedFormGroupComp], + bootstrap: [NestedFormGroupComp] +}) +export class AppModule { +} diff --git a/modules/@angular/examples/forms/ts/nestedFormGroup/nested_form_group_example.ts b/modules/@angular/examples/forms/ts/nestedFormGroup/nested_form_group_example.ts new file mode 100644 index 0000000000..6b7094045d --- /dev/null +++ b/modules/@angular/examples/forms/ts/nestedFormGroup/nested_form_group_example.ts @@ -0,0 +1,52 @@ +/** + * @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 + */ + +// #docregion Component +import {Component} from '@angular/core'; +import {FormControl, FormGroup, Validators} from '@angular/forms'; + +@Component({ + selector: 'example-app', + template: ` +
+ + +`, +}) +export class NestedFormGroupComp { + form = new FormGroup({ + name: new FormGroup({ + first: new FormControl('Nancy', Validators.minLength(2)), + last: new FormControl('Drew', Validators.required) + }), + email: new FormControl() + }); + + get first() { return this.form.get('name.first'); } + + get name() { return this.form.get('name'); } + + onSubmit() { + console.log(this.first.value); // 'Nancy' + console.log(this.name.value); // {first: 'Nancy', last: 'Drew'} + console.log(this.form.value); // {name: {first: 'Nancy', last: 'Drew'}, email: ''} + console.log(this.form.status); // VALID + } + + setPreset() { this.name.setValue({first: 'Bess', last: 'Marvin'}); } +} +// #enddocregion diff --git a/modules/@angular/forms/src/directives/reactive_directives/form_group_name.ts b/modules/@angular/forms/src/directives/reactive_directives/form_group_name.ts index 071d108979..4107cb3206 100644 --- a/modules/@angular/forms/src/directives/reactive_directives/form_group_name.ts +++ b/modules/@angular/forms/src/directives/reactive_directives/form_group_name.ts @@ -24,49 +24,46 @@ export const formGroupNameProvider: any = { }; /** - * Syncs an existing form group to a DOM element. + * @whatItDoes Syncs a nested {@link FormGroup} to a DOM element. * - * This directive can only be used as a child of {@link FormGroupDirective}. It also requires - * importing the {@link ReactiveFormsModule}. + * @howToUse * - * ```typescript - * @Component({ - * selector: 'my-app', - * template: ` - *