linkAbstractControl
npm Package | @angular/forms |
---|---|
Module | import { AbstractControl } from '@angular/forms'; |
Source | forms/src/model.ts |
This is the base class for FormControl
, FormGroup
, and
FormArray
.
It provides some of the shared behavior that all controls and groups of controls have, like
running validators, calculating status, and resetting state. It also defines the properties
that are shared between all sub-classes, like value
, valid
, and dirty
. It shouldn't be
instantiated directly.
linkOverview
class AbstractControl {
constructor(validator: ValidatorFn | null, asyncValidator: AsyncValidatorFn | null)
get value: any
validator: ValidatorFn | null
asyncValidator: AsyncValidatorFn | null
get parent: FormGroup | FormArray
get status: string
get valid: boolean
get invalid: boolean
get pending: boolean
get disabled: boolean
get enabled: boolean
get errors: ValidationErrors | null
get pristine: boolean
get dirty: boolean
get touched: boolean
get untouched: boolean
get valueChanges: Observable<any>
get statusChanges: Observable<any>
get updateOn: FormHooks
setValidators(newValidator: ValidatorFn | ValidatorFn[] | null): void
setAsyncValidators(newValidator: AsyncValidatorFn | AsyncValidatorFn[] | null): void
clearValidators(): void
clearAsyncValidators(): void
markAsTouched(opts: {...}): void
markAsUntouched(opts: {...}): void
markAsDirty(opts: {...}): void
markAsPristine(opts: {...}): void
markAsPending(opts: {...}): void
disable(opts: {...}): void
enable(opts: {...}): void
setParent(parent: FormGroup | FormArray): void
setValue(value: any, options?: Object): void
patchValue(value: any, options?: Object): void
reset(value?: any, options?: Object): void
updateValueAndValidity(opts: {...}): void
setErrors(errors: ValidationErrors | null, opts: {...}): void
get(path: Array<string | number> | string): AbstractControl | null
getError(errorCode: string, path?: string[]): any
hasError(errorCode: string, path?: string[]): boolean
get root: AbstractControl
}
linkSubclasses
linkConstructor
constructor(validator: ValidatorFn | null, asyncValidator: AsyncValidatorFn | null)
linkMembers
validator: ValidatorFn | null
asyncValidator: AsyncValidatorFn | null
get status: string
The validation status of the control. There are four possible validation statuses:
- VALID: control has passed all validation checks
- INVALID: control has failed at least one validation check
- PENDING: control is in the midst of conducting a validation check
- DISABLED: control is exempt from validation checks
These statuses are mutually exclusive, so a control cannot be both valid AND invalid or invalid AND disabled.
get valid: boolean
A control is valid
when its status === VALID
.
In order to have this status, the control must have passed all its validation checks.
get invalid: boolean
A control is invalid
when its status === INVALID
.
In order to have this status, the control must have failed at least one of its validation checks.
get pending: boolean
A control is pending
when its status === PENDING
.
In order to have this status, the control must be in the middle of conducting a validation check.
get disabled: boolean
A control is disabled
when its status === DISABLED
.
Disabled controls are exempt from validation checks and are not included in the aggregate value of their ancestor controls.
get enabled: boolean
A control is enabled
as long as its status !== DISABLED
.
In other words, it has a status of VALID
, INVALID
, or
PENDING
.
get errors: ValidationErrors | null
Returns any errors generated by failing validation. If there are no errors, it will return null.
get pristine: boolean
A control is pristine
if the user has not yet changed
the value in the UI.
Note that programmatic changes to a control's value will not mark it dirty.
get dirty: boolean
A control is dirty
if the user has changed the value
in the UI.
Note that programmatic changes to a control's value will not mark it dirty.
get valueChanges: Observable<any>
Emits an event every time the value of the control changes, in the UI or programmatically.
get statusChanges: Observable<any>
Emits an event every time the validation status of the control is re-calculated.
get updateOn: FormHooks
Returns the update strategy of the AbstractControl
(i.e.
the event on which the control will update itself).
Possible values: 'change'
(default) | 'blur'
| 'submit'
setValidators(newValidator: ValidatorFn | ValidatorFn[] | null): void
Sets the synchronous validators that are active on this control. Calling this will overwrite any existing sync validators.
setAsyncValidators(newValidator: AsyncValidatorFn | AsyncValidatorFn[] | null): void
Sets the async validators that are active on this control. Calling this will overwrite any existing async validators.
markAsTouched(opts: {
onlySelf?: boolean;
} = {}): void
Marks the control as touched
.
This will also mark all direct ancestors as touched
to maintain
the model.
markAsUntouched(opts: {
onlySelf?: boolean;
} = {}): void
Marks the control as untouched
.
If the control has any children, it will also mark all children as untouched
to maintain the model, and re-calculate the touched
status of all parent
controls.
markAsDirty(opts: {
onlySelf?: boolean;
} = {}): void
Marks the control as dirty
.
This will also mark all direct ancestors as dirty
to maintain
the model.
markAsPristine(opts: {
onlySelf?: boolean;
} = {}): void
Marks the control as pristine
.
If the control has any children, it will also mark all children as pristine
to maintain the model, and re-calculate the pristine
status of all parent
controls.
disable(opts: {
onlySelf?: boolean;
emitEvent?: boolean;
} = {}): void
Disables the control. This means the control will be exempt from validation checks and
excluded from the aggregate value of any parent. Its status is DISABLED
.
If the control has children, all children will be disabled to maintain the model.
enable(opts: {
onlySelf?: boolean;
emitEvent?: boolean;
} = {}): void
Enables the control. This means the control will be included in validation checks and the aggregate value of its parent. Its status is re-calculated based on its value and its validators.
If the control has children, all children will be enabled.
setValue(value: any, options?: Object): void
Sets the value of the control. Abstract method (implemented in sub-classes).
patchValue(value: any, options?: Object): void
Patches the value of the control. Abstract method (implemented in sub-classes).
reset(value?: any, options?: Object): void
Resets the control. Abstract method (implemented in sub-classes).
updateValueAndValidity(opts: {
onlySelf?: boolean;
emitEvent?: boolean;
} = {}): void
Re-calculates the value and validation status of the control.
By default, it will also update the value and validity of its ancestors.
setErrors(errors: ValidationErrors | null, opts: {
emitEvent?: boolean;
} = {}): void
Sets errors on a form control.
This is used when validations are run manually by the user, rather than automatically.
Calling setErrors
will also update the validity of the parent control.
linkExample
const login = new FormControl("someLogin");
login.setErrors({
"notUnique": true
});
expect(login.valid).toEqual(false);
expect(login.errors).toEqual({"notUnique": true});
login.setValue("someOtherLogin");
expect(login.valid).toEqual(true);
get(path: Array<string | number> | string): AbstractControl | null
Retrieves a child control given the control's name or path.
Paths can be passed in as an array or a string delimited by a dot.
To get a control nested within a person
sub-group:
this.form.get('person.name');
-OR-
this.form.get(['person', 'name']);
getError(errorCode: string, path?: string[]): any
Returns error data if the control with the given path has the error specified. Otherwise returns null or undefined.
If no path is given, it checks for the error on the present control.
hasError(errorCode: string, path?: string[]): boolean
Returns true if the control with the given path has the error specified. Otherwise returns false.
If no path is given, it checks for the error on the present control.