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
This commit is contained in:
harunurhan 2017-12-03 21:48:45 +01:00 committed by Miško Hevery
parent 71ea931df5
commit 941e88ff79
3 changed files with 26 additions and 4 deletions

View File

@ -64,8 +64,8 @@ export class FormBuilder {
* configuration, with the given optional `validator` and `asyncValidator`.
*/
array(
controlsConfig: any[], validator?: ValidatorFn|null,
asyncValidator?: AsyncValidatorFn|null): FormArray {
controlsConfig: any[], validator?: ValidatorFn|ValidatorFn[]|null,
asyncValidator?: AsyncValidatorFn|AsyncValidatorFn[]|null): FormArray {
const controls = controlsConfig.map(c => this._createControl(c));
return new FormArray(controls, validator, asyncValidator);
}

View File

@ -5,9 +5,10 @@
* 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 {fakeAsync, tick} from '@angular/core/testing';
import {beforeEach, describe, expect, it} from '@angular/core/testing/src/testing_internal';
import {FormBuilder} from '@angular/forms';
import {of } from 'rxjs/observable/of';
(function() {
function syncValidator(_: any /** TODO #9100 */): any /** TODO #9100 */ { return null; }
@ -97,5 +98,26 @@ import {FormBuilder} from '@angular/forms';
expect(a.validator).toBe(syncValidator);
expect(a.asyncValidator).toBe(asyncValidator);
});
it('should create control arrays with multiple async validators', fakeAsync(() => {
function asyncValidator1() { return of ({'async1': true}); }
function asyncValidator2() { return of ({'async2': true}); }
const a = b.array(['one', 'two'], null, [asyncValidator1, asyncValidator2]);
expect(a.value).toEqual(['one', 'two']);
tick();
expect(a.errors).toEqual({'async1': true, 'async2': true});
}));
it('should create control arrays with multiple sync validators', () => {
function syncValidator1() { return {'sync1': true}; }
function syncValidator2() { return {'sync2': true}; }
const a = b.array(['one', 'two'], [syncValidator1, syncValidator2]);
expect(a.value).toEqual(['one', 'two']);
expect(a.errors).toEqual({'sync1': true, 'sync2': true});
});
});
})();

View File

@ -217,7 +217,7 @@ export declare class FormArrayName extends ControlContainer implements OnInit, O
/** @stable */
export declare class FormBuilder {
array(controlsConfig: any[], validator?: ValidatorFn | null, asyncValidator?: AsyncValidatorFn | null): FormArray;
array(controlsConfig: any[], validator?: ValidatorFn | ValidatorFn[] | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null): FormArray;
control(formState: any, validator?: ValidatorFn | ValidatorFn[] | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null): FormControl;
group(controlsConfig: {
[key: string]: any;