Skip to content

Commit

Permalink
feat(forms): multiple validators for array method (angular#20766)
Browse files Browse the repository at this point in the history
Change array method signature so that array of validator and/or async
validatior functions can be passed.

Fixes angular#20665

PR Close angular#20766
  • Loading branch information
harunurhan authored and jbogarthyde committed Feb 23, 2018
1 parent 92a80ca commit d3a8218
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/forms/src/form_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
24 changes: 23 additions & 1 deletion packages/forms/test/form_builder_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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});
});
});
})();
2 changes: 1 addition & 1 deletion tools/public_api_guard/forms/forms.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d3a8218

Please sign in to comment.