fix(forms): allow FormBuilder to create controls with any formState type (#20917)
Align formState type in FormBuilder#control with FormControl#constructor Fixes #20368 PR Close #20917
This commit is contained in:
parent
5713faa667
commit
170885c51b
|
@ -54,7 +54,7 @@ export class FormBuilder {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
control(
|
control(
|
||||||
formState: Object, validator?: ValidatorFn|ValidatorFn[]|null,
|
formState: any, validator?: ValidatorFn|ValidatorFn[]|null,
|
||||||
asyncValidator?: AsyncValidatorFn|AsyncValidatorFn[]|null): FormControl {
|
asyncValidator?: AsyncValidatorFn|AsyncValidatorFn[]|null): FormControl {
|
||||||
return new FormControl(formState, validator, asyncValidator);
|
return new FormControl(formState, validator, asyncValidator);
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,7 @@ import {FormBuilder} from '@angular/forms';
|
||||||
expect(g.controls['password'].asyncValidator).toEqual(asyncValidator);
|
expect(g.controls['password'].asyncValidator).toEqual(asyncValidator);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should use controls', () => {
|
it('should use controls whose form state is a standalone value', () => {
|
||||||
const g = b.group({'login': b.control('some value', syncValidator, asyncValidator)});
|
const g = b.group({'login': b.control('some value', syncValidator, asyncValidator)});
|
||||||
|
|
||||||
expect(g.controls['login'].value).toEqual('some value');
|
expect(g.controls['login'].value).toEqual('some value');
|
||||||
|
@ -49,6 +49,34 @@ import {FormBuilder} from '@angular/forms';
|
||||||
expect(g.controls['login'].asyncValidator).toBe(asyncValidator);
|
expect(g.controls['login'].asyncValidator).toBe(asyncValidator);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should support controls with no validators and whose form state is null', () => {
|
||||||
|
const g = b.group({'login': b.control(null)});
|
||||||
|
expect(g.controls['login'].value).toBeNull();
|
||||||
|
expect(g.controls['login'].validator).toBeNull();
|
||||||
|
expect(g.controls['login'].asyncValidator).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should support controls with validators and whose form state is null', () => {
|
||||||
|
const g = b.group({'login': b.control(null, syncValidator, asyncValidator)});
|
||||||
|
expect(g.controls['login'].value).toBeNull();
|
||||||
|
expect(g.controls['login'].validator).toBe(syncValidator);
|
||||||
|
expect(g.controls['login'].asyncValidator).toBe(asyncValidator);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should support controls with no validators and whose form state is undefined', () => {
|
||||||
|
const g = b.group({'login': b.control(undefined)});
|
||||||
|
expect(g.controls['login'].value).toBeNull();
|
||||||
|
expect(g.controls['login'].validator).toBeNull();
|
||||||
|
expect(g.controls['login'].asyncValidator).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should support controls with validators and whose form state is undefined', () => {
|
||||||
|
const g = b.group({'login': b.control(undefined, syncValidator, asyncValidator)});
|
||||||
|
expect(g.controls['login'].value).toBeNull();
|
||||||
|
expect(g.controls['login'].validator).toBe(syncValidator);
|
||||||
|
expect(g.controls['login'].asyncValidator).toBe(asyncValidator);
|
||||||
|
});
|
||||||
|
|
||||||
it('should create groups with a custom validator', () => {
|
it('should create groups with a custom validator', () => {
|
||||||
const g = b.group(
|
const g = b.group(
|
||||||
{'login': 'some value'}, {'validator': syncValidator, 'asyncValidator': asyncValidator});
|
{'login': 'some value'}, {'validator': syncValidator, 'asyncValidator': asyncValidator});
|
||||||
|
@ -59,10 +87,13 @@ import {FormBuilder} from '@angular/forms';
|
||||||
|
|
||||||
it('should create control arrays', () => {
|
it('should create control arrays', () => {
|
||||||
const c = b.control('three');
|
const c = b.control('three');
|
||||||
|
const e = b.control(null);
|
||||||
|
const f = b.control(undefined);
|
||||||
const a = b.array(
|
const a = b.array(
|
||||||
['one', ['two', syncValidator], c, b.array(['four'])], syncValidator, asyncValidator);
|
['one', ['two', syncValidator], c, b.array(['four']), e, f], syncValidator,
|
||||||
|
asyncValidator);
|
||||||
|
|
||||||
expect(a.value).toEqual(['one', 'two', 'three', ['four']]);
|
expect(a.value).toEqual(['one', 'two', 'three', ['four'], null, null]);
|
||||||
expect(a.validator).toBe(syncValidator);
|
expect(a.validator).toBe(syncValidator);
|
||||||
expect(a.asyncValidator).toBe(asyncValidator);
|
expect(a.asyncValidator).toBe(asyncValidator);
|
||||||
});
|
});
|
||||||
|
|
|
@ -211,7 +211,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 | null, asyncValidator?: AsyncValidatorFn | null): FormArray;
|
||||||
control(formState: Object, 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;
|
||||||
}, extra?: {
|
}, extra?: {
|
||||||
|
|
Loading…
Reference in New Issue