Skip to content

Commit 941e88f

Browse files
harunurhanmhevery
authored andcommitted
feat(forms): multiple validators for array method (#20766)
Change array method signature so that array of validator and/or async validatior functions can be passed. Fixes #20665 PR Close #20766
1 parent 71ea931 commit 941e88f

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

packages/forms/src/form_builder.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ export class FormBuilder {
6464
* configuration, with the given optional `validator` and `asyncValidator`.
6565
*/
6666
array(
67-
controlsConfig: any[], validator?: ValidatorFn|null,
68-
asyncValidator?: AsyncValidatorFn|null): FormArray {
67+
controlsConfig: any[], validator?: ValidatorFn|ValidatorFn[]|null,
68+
asyncValidator?: AsyncValidatorFn|AsyncValidatorFn[]|null): FormArray {
6969
const controls = controlsConfig.map(c => this._createControl(c));
7070
return new FormArray(controls, validator, asyncValidator);
7171
}

packages/forms/test/form_builder_spec.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
8+
import {fakeAsync, tick} from '@angular/core/testing';
99
import {beforeEach, describe, expect, it} from '@angular/core/testing/src/testing_internal';
1010
import {FormBuilder} from '@angular/forms';
11+
import {of } from 'rxjs/observable/of';
1112

1213
(function() {
1314
function syncValidator(_: any /** TODO #9100 */): any /** TODO #9100 */ { return null; }
@@ -97,5 +98,26 @@ import {FormBuilder} from '@angular/forms';
9798
expect(a.validator).toBe(syncValidator);
9899
expect(a.asyncValidator).toBe(asyncValidator);
99100
});
101+
102+
it('should create control arrays with multiple async validators', fakeAsync(() => {
103+
function asyncValidator1() { return of ({'async1': true}); }
104+
function asyncValidator2() { return of ({'async2': true}); }
105+
106+
const a = b.array(['one', 'two'], null, [asyncValidator1, asyncValidator2]);
107+
expect(a.value).toEqual(['one', 'two']);
108+
109+
tick();
110+
111+
expect(a.errors).toEqual({'async1': true, 'async2': true});
112+
}));
113+
114+
it('should create control arrays with multiple sync validators', () => {
115+
function syncValidator1() { return {'sync1': true}; }
116+
function syncValidator2() { return {'sync2': true}; }
117+
118+
const a = b.array(['one', 'two'], [syncValidator1, syncValidator2]);
119+
expect(a.value).toEqual(['one', 'two']);
120+
expect(a.errors).toEqual({'sync1': true, 'sync2': true});
121+
});
100122
});
101123
})();

tools/public_api_guard/forms/forms.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export declare class FormArrayName extends ControlContainer implements OnInit, O
217217

218218
/** @stable */
219219
export declare class FormBuilder {
220-
array(controlsConfig: any[], validator?: ValidatorFn | null, asyncValidator?: AsyncValidatorFn | null): FormArray;
220+
array(controlsConfig: any[], validator?: ValidatorFn | ValidatorFn[] | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null): FormArray;
221221
control(formState: any, validator?: ValidatorFn | ValidatorFn[] | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null): FormControl;
222222
group(controlsConfig: {
223223
[key: string]: any;

0 commit comments

Comments
 (0)