Reactive Form
About
The Form is created programmatically and syncronized with the DOM.
Some advantages are:
It is build around Observable streams.
It is more roboust, scalable, reusable and testable.
It uses synchronous data-flow between View and Data models, which makes creating large-scale forms easier.
(View-to-Model) and (Model-to-view) flow. (more info)
Keep the data model pure by providing it as an immutable data structure. So each time a change is triggered on the data model, the
FormControlinstance returns a new data model rather than updating the existing one.This gives the ability to track unique changes to the data model through the control's Observable.
Then change detection is more efficient because it only needs to update on unique changes.
Can have Custom validation functions.
Testing can be done without renderding the UI, in these tests, status and data are queried and manipulated through the control without interacting the the change detection cycle. (more info)
Examples
Strictly Typing Setup
You may strictly type the controls if needed with:
Simple Setup - Just FormControl without Services
FormControl without ServicesBy default FormControl create typed form controls based on the initial value.
To use untyped form controls you must use UntypedFormControl.
Simple Setup - FormGroup without Services
FormGroup without ServicesTo Submit forms, use ngSubmit in the Template.
To programatically change form control values, you can use:
setValue: Must update the holeFormGroupobject, even if only updating one of the form controls.patchValue: Can update one or multiple form controls.
Nested Setup - FormGroup without Services
FormGroup without ServicesSimple Setup - FormGroup with Service: FormBuilder
FormGroup with Service: FormBuilderControls created with FormBuilder will be automatically typed based on the initial value. (To use untyped forms you must explicitly use the UntypedFormBuilder)
formBuilder has 4 functions:
group(): To createFormGroup.First parameter, a collection of child controls
FormControl.Second Parameter, object of options
AbstractControlOptionscontaining:validators: Synchronous validators that will be ran for the Group. (For multiple controls)asyncValidators: Aynsc validators that will be ran for the Group. (For multiple controls)updateOn: The event upon which the control should be updated. ('change' | 'blur' | 'submit')
record(): To createFormRecord.First paremeter, a collection of child controls
FormControl.Second Parameter, object of options
AbstractControlOptionscontaining:validators: Synchronous validators that will be ran for the Group. (For multiple controls)asyncValidators: Aynsc validators that will be ran for the Group. (For multiple controls)updateOn: The event upon which the control should be updated. ('change' | 'blur' | 'submit')
control(): To createFormControl.First parameter, the initial value for the control,
Second parameter,
ValidatorFnorValidatorFn[]orFormControlOptions.Third parameter,
AsyncValidatorFnorAsyncValidatorFn[].
array(): To createFormArray.First parameter, an Array of child Controls.
Second parameter,
ValidatorFnorValidatorFn[]orFormControlOptions.Third parameter,
AsyncValidatorFnorAsyncValidatorFn[].
Validation Setup - FormGroup
FormGroupValidators are passed as second parameter, after the initial value.
Async Validators are passed as third parameter.
Form validation status, can be accessed by formVariable.status.
You can also access each form control status with:
formVariable.controls.<formControlName>.invalidformVariable.controls.get('formControlName').invalid
Dynamic Setup - FormArray with Service: FormBuilder
FormArray with Service: FormBuilderThe FormArray instance represents an undefined number of controls in an array.
Each FormControl created will be referenced by its index in the FormArray.
It can be useful for creating dynamic form inputs or managing checkboxes or radio buttons.
NonNullable Setup - FormControl
FormControlWhen reseting the form, the initial values will be used by default.
You may defined non-nullable form controls with:
Or
Or
Subscribing to statusChanges or valueChanges Observables - Reactive value changes
statusChanges or valueChanges Observables - Reactive value changesFor instance saving the form values in the localStorage after 500ms.
Custom Validators Setup - Synchronous
SynchronousFunctions that take a control instance and immediately return either a set of validation errors or null.
Cross-field Validation
You add the validator method to the group.
Custom Validators Setup - Async
AsyncFunctions that take a control instance and must return a Promise or Observable that later emits a set of validations errors or null.
For performance reasons, Angular only runs async validators after all sync validators pass.
The Validator functions takes the same control: AbstractControl param.
For intance reaching to a webserver to validate:
Last updated
