Skip to main content
The FormControl class tracks the value and validation status of an individual form control. It is one of the four fundamental building blocks of Angular forms, along with FormGroup, FormArray, and FormRecord.

Import

Constructor

T | FormControlState<T>
default:"null"
Initial value for the control, or an object that defines the initial value and disabled state.
ValidatorFn | ValidatorFn[] | FormControlOptions | null
A synchronous validator function, an array of such functions, or a FormControlOptions object that contains validation functions and a validation trigger.
AsyncValidatorFn | AsyncValidatorFn[] | null
A single async validator or array of async validator functions.

Basic Usage

Creating a FormControl

Type Safety

FormControl accepts a generic type argument:

Properties

T
The current value of the control.
'VALID' | 'INVALID' | 'PENDING' | 'DISABLED'
The validation status of the control.
boolean
A control is valid when its status is VALID.
boolean
A control is invalid when its status is INVALID.
boolean
A control is pending when its status is PENDING.
boolean
A control is disabled when its status is DISABLED.
boolean
A control is enabled as long as its status is not DISABLED.
ValidationErrors | null
An object containing any errors generated by failing validation, or null if there are no errors.
boolean
A control is pristine if the user has not yet changed the value in the UI.
boolean
A control is dirty if the user has changed the value in the UI.
boolean
True if the control is marked as touched.
boolean
True if the control has not been marked as touched.
T
The default value of this FormControl, used whenever the control is reset without an explicit value.
Observable<T>
A multicasting observable that emits an event every time the value of the control changes.
Observable<FormControlStatus>
A multicasting observable that emits an event every time the validation status of the control recalculates.

Methods

setValue()

Sets a new value for the form control.
T
required
The new value for the control.
boolean
default:"false"
When true, each change only affects this control, and not its parent.
boolean
default:"true"
When true, both the statusChanges and valueChanges observables emit events with the latest status and value.
boolean
default:"true"
When true, each change triggers an onChange event to update the view.
boolean
default:"true"
When true, each change triggers an ngModelChange event to update the model.

patchValue()

Patches the value of the control. For FormControl, this is functionally the same as setValue().

reset()

Resets the form control, marking it pristine and untouched, and resetting the value.
T | FormControlState<T>
Resets the control with an initial value, or an object that defines the initial value and disabled state. If not provided, resets to null or the default value if nonNullable was set.
boolean
default:"false"
When true, each change only affects this control, and not its parent.
boolean
default:"true"
When true, both the statusChanges and valueChanges observables emit events.

getRawValue()

Returns the value of the control. For FormControl, the raw value is equivalent to the value.

disable()

Disables the control, meaning it will be exempt from validation checks and excluded from aggregate values of parent controls.
boolean
default:"false"
When true, mark only this control. When false, marks all direct ancestors.
boolean
default:"true"
When true, emit a statusChanges event.

enable()

Enables the control.

markAsTouched()

Marks the control as touched.

markAsUntouched()

Marks the control as untouched.

markAsDirty()

Marks the control as dirty.

markAsPristine()

Marks the control as pristine.

updateValueAndValidity()

Recalculates the value and validation status of the control.

setValidators()

Sets the synchronous validators that are active on this control.
ValidatorFn | ValidatorFn[] | null
required
The new validator or validators.

setAsyncValidators()

Sets the asynchronous validators that are active on this control.

addValidators()

Adds validators to the control.

removeValidators()

Removes validators from the control.

hasError()

Reports whether the control has the error specified.
string
required
The error code to check for.
string | (string | number)[]
Path to check (used in FormGroup/FormArray).

getError()

Retrieves the error object for the specified error code.

FormControlOptions

Options object for configuring a FormControl.
ValidatorFn | ValidatorFn[]
A synchronous validator function, or an array of such functions.
AsyncValidatorFn | AsyncValidatorFn[]
A single async validator or array of async validator functions.
'change' | 'blur' | 'submit'
default:"'change'"
The event on which the control should update its value and validity.
boolean
default:"false"
Whether to use the initial value as the default value. When true, the control will reset to its initial value instead of null.

FormControlState

Interface for defining both a value and disabled state for a FormControl.
T
required
The value of the control.
boolean
required
Whether the control is disabled.

Examples

Non-Nullable FormControl

Update on Blur

Listening to Value Changes

Custom Async Validator

See Also