* @description * Constructs a new `FormControl` with the given state, validators and options. Sets * `{nonNullable: true}` in the options to get a non-nullable control. Otherwise, the * control will be nullable. Accepts a single generic argument, which is the type of the * control's val
(
formState: T | FormControlState<T>,
validatorOrOpts?: ValidatorFn | ValidatorFn[] | FormControlOptions | null,
asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null,
)
| 364 | * {@example forms/ts/formBuilder/form_builder_example.ts region='disabled-control'} |
| 365 | */ |
| 366 | control<T>( |
| 367 | formState: T | FormControlState<T>, |
| 368 | validatorOrOpts?: ValidatorFn | ValidatorFn[] | FormControlOptions | null, |
| 369 | asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null, |
| 370 | ): FormControl { |
| 371 | let newOptions: FormControlOptions = {}; |
| 372 | if (!this.useNonNullable) { |
| 373 | return new FormControl(formState, validatorOrOpts, asyncValidator); |
| 374 | } |
| 375 | if (isAbstractControlOptions(validatorOrOpts)) { |
| 376 | // If the second argument is options, then they are copied. |
| 377 | newOptions = validatorOrOpts; |
| 378 | } else { |
| 379 | // If the other arguments are validators, they are copied into an options object. |
| 380 | newOptions.validators = validatorOrOpts; |
| 381 | newOptions.asyncValidators = asyncValidator; |
| 382 | } |
| 383 | return new FormControl<T>(formState, {...newOptions, nonNullable: true}); |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Constructs a new `FormArray` from the given array of configurations, |
no test coverage detected