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:
parent
71ea931df5
commit
941e88ff79
@ -64,8 +64,8 @@ export class FormBuilder {
|
|||||||
* configuration, with the given optional `validator` and `asyncValidator`.
|
* configuration, with the given optional `validator` and `asyncValidator`.
|
||||||
*/
|
*/
|
||||||
array(
|
array(
|
||||||
controlsConfig: any[], validator?: ValidatorFn|null,
|
controlsConfig: any[], validator?: ValidatorFn|ValidatorFn[]|null,
|
||||||
asyncValidator?: AsyncValidatorFn|null): FormArray {
|
asyncValidator?: AsyncValidatorFn|AsyncValidatorFn[]|null): FormArray {
|
||||||
const controls = controlsConfig.map(c => this._createControl(c));
|
const controls = controlsConfig.map(c => this._createControl(c));
|
||||||
return new FormArray(controls, validator, asyncValidator);
|
return new FormArray(controls, validator, asyncValidator);
|
||||||
}
|
}
|
||||||
|
@ -5,9 +5,10 @@
|
|||||||
* Use of this source code is governed by an MIT-style license that can be
|
* 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
|
* 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 {beforeEach, describe, expect, it} from '@angular/core/testing/src/testing_internal';
|
||||||
import {FormBuilder} from '@angular/forms';
|
import {FormBuilder} from '@angular/forms';
|
||||||
|
import {of } from 'rxjs/observable/of';
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
function syncValidator(_: any /** TODO #9100 */): any /** TODO #9100 */ { return null; }
|
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.validator).toBe(syncValidator);
|
||||||
expect(a.asyncValidator).toBe(asyncValidator);
|
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});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
|
2
tools/public_api_guard/forms/forms.d.ts
vendored
2
tools/public_api_guard/forms/forms.d.ts
vendored
@ -217,7 +217,7 @@ export declare class FormArrayName extends ControlContainer implements OnInit, O
|
|||||||
|
|
||||||
/** @stable */
|
/** @stable */
|
||||||
export declare class FormBuilder {
|
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;
|
control(formState: any, validator?: ValidatorFn | ValidatorFn[] | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null): FormControl;
|
||||||
group(controlsConfig: {
|
group(controlsConfig: {
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user