2016-06-23 09:47:54 -07:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 12:08:49 -07:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-06-23 09:47:54 -07:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2016-06-08 15:36:24 -07:00
|
|
|
import {Injectable} from '@angular/core';
|
2016-06-08 16:38:52 -07:00
|
|
|
|
|
|
|
import {AsyncValidatorFn, ValidatorFn} from './directives/validators';
|
2021-03-08 17:51:26 -08:00
|
|
|
import {ReactiveFormsModule} from './form_providers';
|
2018-06-20 19:21:46 +02:00
|
|
|
import {AbstractControl, AbstractControlOptions, FormArray, FormControl, FormGroup, FormHooks} from './model';
|
2016-06-08 16:38:52 -07:00
|
|
|
|
2020-04-06 15:44:00 -07:00
|
|
|
function isAbstractControlOptions(options: AbstractControlOptions|
|
|
|
|
{[key: string]: any}): options is AbstractControlOptions {
|
2018-11-07 20:38:07 +01:00
|
|
|
return (<AbstractControlOptions>options).asyncValidators !== undefined ||
|
|
|
|
(<AbstractControlOptions>options).validators !== undefined ||
|
|
|
|
(<AbstractControlOptions>options).updateOn !== undefined;
|
|
|
|
}
|
|
|
|
|
2016-06-08 15:36:24 -07:00
|
|
|
/**
|
2018-04-05 11:02:42 +01:00
|
|
|
* @description
|
2018-04-05 10:58:12 +01:00
|
|
|
* Creates an `AbstractControl` from a user-specified configuration.
|
2016-09-13 13:23:31 -07:00
|
|
|
*
|
2018-06-27 14:33:16 -05:00
|
|
|
* The `FormBuilder` provides syntactic sugar that shortens creating instances of a `FormControl`,
|
|
|
|
* `FormGroup`, or `FormArray`. It reduces the amount of boilerplate needed to build complex
|
2016-09-13 13:23:31 -07:00
|
|
|
* forms.
|
|
|
|
*
|
2018-06-27 14:33:16 -05:00
|
|
|
* @see [Reactive Forms Guide](/guide/reactive-forms)
|
2018-04-05 22:31:44 +01:00
|
|
|
*
|
2018-10-19 17:36:24 +01:00
|
|
|
* @publicApi
|
2016-06-08 15:36:24 -07:00
|
|
|
*/
|
2021-03-08 17:51:26 -08:00
|
|
|
@Injectable({providedIn: ReactiveFormsModule})
|
2016-06-08 15:36:24 -07:00
|
|
|
export class FormBuilder {
|
|
|
|
/**
|
2018-06-27 14:33:16 -05:00
|
|
|
* @description
|
|
|
|
* Construct a new `FormGroup` instance.
|
|
|
|
*
|
|
|
|
* @param controlsConfig A collection of child controls. The key for each child is the name
|
|
|
|
* under which it is registered.
|
|
|
|
*
|
2020-11-19 12:29:20 -08:00
|
|
|
* @param options Configuration options object for the `FormGroup`. The object should have the
|
|
|
|
* the `AbstractControlOptions` type and might contain the following fields:
|
2018-11-06 14:11:14 -08:00
|
|
|
* * `validators`: A synchronous validator function, or an array of validator functions
|
|
|
|
* * `asyncValidators`: A single async validator or array of async validator functions
|
|
|
|
* * `updateOn`: The event upon which the control should be updated (options: 'change' | 'blur' |
|
|
|
|
* submit')
|
2020-11-19 12:29:20 -08:00
|
|
|
*/
|
|
|
|
group(
|
|
|
|
controlsConfig: {[key: string]: any},
|
|
|
|
options?: AbstractControlOptions|null,
|
|
|
|
): FormGroup;
|
|
|
|
/**
|
|
|
|
* @description
|
|
|
|
* Construct a new `FormGroup` instance.
|
2018-11-06 14:11:14 -08:00
|
|
|
*
|
2020-12-03 13:11:41 +01:00
|
|
|
* @deprecated This API is not typesafe and can result in issues with Closure Compiler renaming.
|
|
|
|
* Use the `FormBuilder#group` overload with `AbstractControlOptions` instead.
|
|
|
|
* Note that `AbstractControlOptions` expects `validators` and `asyncValidators` to be valid
|
|
|
|
* validators. If you have custom validators, make sure their validation function parameter is
|
|
|
|
* `AbstractControl` and not a sub-class, such as `FormGroup`. These functions will be called with
|
|
|
|
* an object of type `AbstractControl` and that cannot be automatically downcast to a subclass, so
|
|
|
|
* TypeScript sees this as an error. For example, change the `(group: FormGroup) =>
|
|
|
|
* ValidationErrors|null` signature to be `(group: AbstractControl) => ValidationErrors|null`.
|
2020-11-19 12:29:20 -08:00
|
|
|
*
|
|
|
|
* @param controlsConfig A collection of child controls. The key for each child is the name
|
|
|
|
* under which it is registered.
|
|
|
|
*
|
|
|
|
* @param options Configuration options object for the `FormGroup`. The legacy configuration
|
|
|
|
* object consists of:
|
2018-06-27 14:33:16 -05:00
|
|
|
* * `validator`: A synchronous validator function, or an array of validator functions
|
|
|
|
* * `asyncValidator`: A single async validator or array of async validator functions
|
2020-11-19 12:29:20 -08:00
|
|
|
* Note: the legacy format is deprecated and might be removed in one of the next major versions
|
|
|
|
* of Angular.
|
2016-06-08 15:36:24 -07:00
|
|
|
*/
|
2020-11-19 12:29:20 -08:00
|
|
|
group(
|
|
|
|
controlsConfig: {[key: string]: any},
|
|
|
|
options: {[key: string]: any},
|
|
|
|
): FormGroup;
|
2018-11-07 20:38:07 +01:00
|
|
|
group(
|
|
|
|
controlsConfig: {[key: string]: any},
|
|
|
|
options: AbstractControlOptions|{[key: string]: any}|null = null): FormGroup {
|
2016-08-24 16:58:43 -07:00
|
|
|
const controls = this._reduceControls(controlsConfig);
|
2018-06-20 19:21:46 +02:00
|
|
|
|
|
|
|
let validators: ValidatorFn|ValidatorFn[]|null = null;
|
|
|
|
let asyncValidators: AsyncValidatorFn|AsyncValidatorFn[]|null = null;
|
|
|
|
let updateOn: FormHooks|undefined = undefined;
|
|
|
|
|
2018-11-07 20:38:07 +01:00
|
|
|
if (options != null) {
|
|
|
|
if (isAbstractControlOptions(options)) {
|
|
|
|
// `options` are `AbstractControlOptions`
|
|
|
|
validators = options.validators != null ? options.validators : null;
|
|
|
|
asyncValidators = options.asyncValidators != null ? options.asyncValidators : null;
|
|
|
|
updateOn = options.updateOn != null ? options.updateOn : undefined;
|
|
|
|
} else {
|
|
|
|
// `options` are legacy form group options
|
2019-02-22 14:30:10 -08:00
|
|
|
validators = options['validator'] != null ? options['validator'] : null;
|
|
|
|
asyncValidators = options['asyncValidator'] != null ? options['asyncValidator'] : null;
|
2018-11-07 20:38:07 +01:00
|
|
|
}
|
2018-06-20 19:21:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return new FormGroup(controls, {asyncValidators, updateOn, validators});
|
2016-06-08 15:36:24 -07:00
|
|
|
}
|
2018-06-27 14:33:16 -05:00
|
|
|
|
2016-06-08 15:36:24 -07:00
|
|
|
/**
|
2018-06-27 14:33:16 -05:00
|
|
|
* @description
|
2018-06-20 19:21:46 +02:00
|
|
|
* Construct a new `FormControl` with the given state, validators and options.
|
2018-06-27 14:33:16 -05:00
|
|
|
*
|
2018-06-20 19:21:46 +02:00
|
|
|
* @param formState Initializes the control with an initial state value, or
|
|
|
|
* with an object that contains both a value and a disabled status.
|
2016-09-13 13:23:31 -07:00
|
|
|
*
|
2018-06-20 19:21:46 +02:00
|
|
|
* @param validatorOrOpts A synchronous validator function, or an array of
|
|
|
|
* such functions, or an `AbstractControlOptions` object that contains
|
|
|
|
* validation functions and a validation trigger.
|
2018-06-27 14:33:16 -05:00
|
|
|
*
|
2018-06-20 19:21:46 +02:00
|
|
|
* @param asyncValidator A single async validator or array of async validator
|
|
|
|
* functions.
|
2018-06-27 14:33:16 -05:00
|
|
|
*
|
|
|
|
* @usageNotes
|
|
|
|
*
|
|
|
|
* ### Initialize a control as disabled
|
|
|
|
*
|
|
|
|
* The following example returns a control with an initial value in a disabled state.
|
|
|
|
*
|
2019-07-20 20:40:17 +03:00
|
|
|
* <code-example path="forms/ts/formBuilder/form_builder_example.ts" region="disabled-control">
|
2018-06-27 14:33:16 -05:00
|
|
|
* </code-example>
|
2016-06-08 15:36:24 -07:00
|
|
|
*/
|
2016-06-13 11:27:04 -07:00
|
|
|
control(
|
2018-06-20 19:21:46 +02:00
|
|
|
formState: any, validatorOrOpts?: ValidatorFn|ValidatorFn[]|AbstractControlOptions|null,
|
2017-04-17 11:13:30 -07:00
|
|
|
asyncValidator?: AsyncValidatorFn|AsyncValidatorFn[]|null): FormControl {
|
2018-06-20 19:21:46 +02:00
|
|
|
return new FormControl(formState, validatorOrOpts, asyncValidator);
|
2016-06-08 15:36:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-20 19:21:46 +02:00
|
|
|
* Constructs a new `FormArray` from the given array of configurations,
|
|
|
|
* validators and options.
|
2018-06-27 14:33:16 -05:00
|
|
|
*
|
2018-06-20 19:21:46 +02:00
|
|
|
* @param controlsConfig An array of child controls or control configs. Each
|
|
|
|
* child control is given an index when it is registered.
|
2018-06-27 14:33:16 -05:00
|
|
|
*
|
2018-06-20 19:21:46 +02:00
|
|
|
* @param validatorOrOpts A synchronous validator function, or an array of
|
|
|
|
* such functions, or an `AbstractControlOptions` object that contains
|
|
|
|
* validation functions and a validation trigger.
|
2018-06-27 14:33:16 -05:00
|
|
|
*
|
2018-06-20 19:21:46 +02:00
|
|
|
* @param asyncValidator A single async validator or array of async validator
|
|
|
|
* functions.
|
2016-06-08 15:36:24 -07:00
|
|
|
*/
|
2016-06-08 16:38:52 -07:00
|
|
|
array(
|
2018-06-20 19:21:46 +02:00
|
|
|
controlsConfig: any[],
|
|
|
|
validatorOrOpts?: ValidatorFn|ValidatorFn[]|AbstractControlOptions|null,
|
2017-12-03 21:48:45 +01:00
|
|
|
asyncValidator?: AsyncValidatorFn|AsyncValidatorFn[]|null): FormArray {
|
2016-11-12 14:08:58 +01:00
|
|
|
const controls = controlsConfig.map(c => this._createControl(c));
|
2018-06-20 19:21:46 +02:00
|
|
|
return new FormArray(controls, validatorOrOpts, asyncValidator);
|
2016-06-08 15:36:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
2016-06-22 14:56:10 -07:00
|
|
|
_reduceControls(controlsConfig: {[k: string]: any}): {[key: string]: AbstractControl} {
|
2016-11-12 14:08:58 +01:00
|
|
|
const controls: {[key: string]: AbstractControl} = {};
|
2016-10-03 16:46:05 -07:00
|
|
|
Object.keys(controlsConfig).forEach(controlName => {
|
|
|
|
controls[controlName] = this._createControl(controlsConfig[controlName]);
|
2016-06-08 15:36:24 -07:00
|
|
|
});
|
|
|
|
return controls;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
2016-06-22 14:56:10 -07:00
|
|
|
_createControl(controlConfig: any): AbstractControl {
|
|
|
|
if (controlConfig instanceof FormControl || controlConfig instanceof FormGroup ||
|
|
|
|
controlConfig instanceof FormArray) {
|
2016-06-08 15:36:24 -07:00
|
|
|
return controlConfig;
|
|
|
|
|
2016-10-19 13:42:39 -07:00
|
|
|
} else if (Array.isArray(controlConfig)) {
|
|
|
|
const value = controlConfig[0];
|
|
|
|
const validator: ValidatorFn = controlConfig.length > 1 ? controlConfig[1] : null;
|
|
|
|
const asyncValidator: AsyncValidatorFn = controlConfig.length > 2 ? controlConfig[2] : null;
|
2016-06-08 15:36:24 -07:00
|
|
|
return this.control(value, validator, asyncValidator);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return this.control(controlConfig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|