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 infoarrow-up-right)

  • 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 FormControl instance 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 infoarrow-up-right)

Examples

Strictly Typing Setup

You may strictly type the controls if needed with:

Simple Setup - Just FormControl without Services

By 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

To Submit forms, use ngSubmit in the Template.

To programatically change form control values, you can use:

  • setValue: Must update the hole FormGroup object, even if only updating one of the form controls.

  • patchValue: Can update one or multiple form controls.

Nested Setup - FormGroup without Services

Simple Setup - FormGroup with Service: FormBuilder

forms/form-builder

Controls 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 create FormGroup.

    1. First parameter, a collection of child controls FormControl.

    2. Second Parameter, object of options AbstractControlOptions containing:

      • 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 create FormRecord.

    1. First paremeter, a collection of child controls FormControl.

    2. Second Parameter, object of options AbstractControlOptions containing:

      • 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 create FormControl.

    1. First parameter, the initial value for the control,

    2. Second parameter, ValidatorFn or ValidatorFn[] or FormControlOptions.

    3. Third parameter, AsyncValidatorFn or AsyncValidatorFn[].

  • array(): To create FormArray.

    1. First parameter, an Array of child Controls.

    2. Second parameter, ValidatorFn or ValidatorFn[] or FormControlOptions.

    3. Third parameter, AsyncValidatorFn or AsyncValidatorFn[].

Validation Setup - FormGroup

forms/validators

Validators 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>.invalid

  • formVariable.controls.get('formControlName').invalid

Dynamic Setup - FormArray with Service: FormBuilder

The 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

When 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

For instance saving the form values in the localStorage after 500ms.

Custom Validators Setup - Synchronous

forms/forms-validators#custom-validators

Functions 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

forms/forms-validators#async-validators

Functions 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