Skip to content

Commit 1ece736

Browse files
Dzmitry Shylovichmhevery
authored andcommitted
fix(forms): getRawValue should correctly work with nested FormGroups/Arrays (angular#12964)
Closed angular#12963 PR Close angular#12964
1 parent 7ac38aa commit 1ece736

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

modules/@angular/forms/src/model.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,7 @@ export class FormGroup extends AbstractControl {
10201020
getRawValue(): any {
10211021
return this._reduceChildren(
10221022
{}, (acc: {[k: string]: AbstractControl}, control: AbstractControl, name: string) => {
1023-
acc[name] = control.value;
1023+
acc[name] = control instanceof FormControl ? control.value : (<any>control).getRawValue();
10241024
return acc;
10251025
});
10261026
}
@@ -1321,7 +1321,11 @@ export class FormArray extends AbstractControl {
13211321
* If you'd like to include all values regardless of disabled status, use this method.
13221322
* Otherwise, the `value` property is the best way to get the value of the array.
13231323
*/
1324-
getRawValue(): any[] { return this.controls.map((control) => control.value); }
1324+
getRawValue(): any[] {
1325+
return this.controls.map((control: AbstractControl) => {
1326+
return control instanceof FormControl ? control.value : (<any>control).getRawValue();
1327+
});
1328+
}
13251329

13261330
/** @internal */
13271331
_throwIfControlMissing(index: number): void {

modules/@angular/forms/test/form_array_spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function main() {
3232
}
3333

3434
describe('FormArray', () => {
35+
3536
describe('adding/removing', () => {
3637
let a: FormArray;
3738
let c1: FormControl, c2: FormControl, c3: FormControl;
@@ -81,6 +82,21 @@ export function main() {
8182
});
8283
});
8384

85+
describe('getRawValue()', () => {
86+
let a: FormArray;
87+
88+
it('should work with nested form groups/arrays', () => {
89+
a = new FormArray([
90+
new FormGroup({'c2': new FormControl('v2'), 'c3': new FormControl('v3')}),
91+
new FormArray([new FormControl('v4'), new FormControl('v5')])
92+
]);
93+
a.at(0).get('c3').disable();
94+
(a.at(1) as FormArray).at(1).disable();
95+
96+
expect(a.getRawValue()).toEqual([{'c2': 'v2', 'c3': 'v3'}, ['v4', 'v5']]);
97+
});
98+
});
99+
84100
describe('setValue', () => {
85101
let c: FormControl, c2: FormControl, a: FormArray;
86102

modules/@angular/forms/test/form_group_spec.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {async, fakeAsync, tick} from '@angular/core/testing';
1010
import {AsyncTestCompleter, beforeEach, describe, inject, it} from '@angular/core/testing/testing_internal';
11-
import {AbstractControl, FormControl, FormGroup, Validators} from '@angular/forms';
11+
import {AbstractControl, FormArray, FormControl, FormGroup, Validators} from '@angular/forms';
1212

1313
import {EventEmitter} from '../src/facade/async';
1414
import {isPresent} from '../src/facade/lang';
@@ -62,6 +62,24 @@ export function main() {
6262
});
6363
});
6464

65+
describe('getRawValue', () => {
66+
let fg: FormGroup;
67+
68+
it('should work with nested form groups/arrays', () => {
69+
fg = new FormGroup({
70+
'c1': new FormControl('v1'),
71+
'group': new FormGroup({'c2': new FormControl('v2'), 'c3': new FormControl('v3')}),
72+
'array': new FormArray([new FormControl('v4'), new FormControl('v5')])
73+
});
74+
fg.get('group').get('c3').disable();
75+
(fg.get('array') as FormArray).at(1).disable();
76+
77+
expect(fg.getRawValue())
78+
.toEqual({'c1': 'v1', 'group': {'c2': 'v2', 'c3': 'v3'}, 'array': ['v4', 'v5']});
79+
});
80+
81+
});
82+
6583
describe('adding and removing controls', () => {
6684
it('should update value and validity when control is added', () => {
6785
const g = new FormGroup({'one': new FormControl('1')});

0 commit comments

Comments
 (0)