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
Initial value for the control, or an object that defines the initial value and disabled state.
A synchronous validator function, an array of such functions, or a
FormControlOptions object that contains validation functions and a validation trigger.A single async validator or array of async validator functions.
Basic Usage
Creating a FormControl
Type Safety
FormControl accepts a generic type argument:
Properties
The current value of the control.
The validation status of the control.
A control is valid when its status is
VALID.A control is invalid when its status is
INVALID.A control is pending when its status is
PENDING.A control is disabled when its status is
DISABLED.A control is enabled as long as its status is not
DISABLED.An object containing any errors generated by failing validation, or null if there are no errors.
A control is pristine if the user has not yet changed the value in the UI.
A control is dirty if the user has changed the value in the UI.
True if the control is marked as touched.
True if the control has not been marked as touched.
The default value of this FormControl, used whenever the control is reset without an explicit value.
A multicasting observable that emits an event every time the value of the control changes.
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.The new value for the control.
When true, each change only affects this control, and not its parent.
When true, both the
statusChanges and valueChanges observables emit events with the latest status and value.When true, each change triggers an
onChange event to update the view.When true, each change triggers an
ngModelChange event to update the model.patchValue()
Patches the value of the control. ForFormControl, this is functionally the same as setValue().
reset()
Resets the form control, marking itpristine and untouched, and resetting the value.
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.When true, each change only affects this control, and not its parent.
When true, both the
statusChanges and valueChanges observables emit events.getRawValue()
Returns the value of the control. ForFormControl, 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.When true, mark only this control. When false, marks all direct ancestors.
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.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.The error code to check for.
Path to check (used in FormGroup/FormArray).
getError()
Retrieves the error object for the specified error code.FormControlOptions
Options object for configuring aFormControl.
A synchronous validator function, or an array of such functions.
A single async validator or array of async validator functions.
The event on which the control should update its value and validity.
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 aFormControl.
The value of the control.
Whether the control is disabled.
Examples
Non-Nullable FormControl
Update on Blur
Listening to Value Changes
Custom Async Validator
See Also
- FormGroup - Group multiple form controls
- FormArray - Manage an array of form controls
- Validators - Built-in validation functions
- Reactive Forms Guide
- Form Validation Guide
