angular form validation

Angular Form Validation

Angular Form Validation possesses a module that exploits process forms. Program AngularJS checks if the shape is valid. There’re two options for working with forms offered by Angular Form Validation:

  1. Sample-based approach (template-driven).
  2. Reactive (reactive) approach.

The pattern approach is more hereditary for a declarative approach to a framework. The reactive substance was found later and is progressive and lissom. In order to commence working with forms, you have to link a form module (FormsModule). It looks like this:

import {FormsModule} from ‘@ angular / forms’; 

If there is a need for a reactive module, then connect ReactiveFormsModule:

import {ReactiveFormsModule} from ‘@ angular / forms’; 

Once the module we require is plugged in, we insert it into the main module. There are three primary concepts in the template approach:

  1. Ngmodel
  2. Ngmodelgroup
  3. Ngform..

The ngModel is exploit to attach data to a form element.  Visually it looks like this:

<input type = “text” [ngModel] = “name”>

You can get input data in three ways: double-sided binding, event handling,  to an element.

The pattern approach accomplishes HTML5 validation. For an Angular form validation sample can be exploit as the needed attribute.

You also have an capability to erect your own validators in a template approach.  To execute this operation, create a directive to complement the standard Ng_Validators validators with the help of «multi».

In order to determine the error, you can exploit ngModel and receive the «errors» option. In the reactive approach, the elements of the form are set up in the controller, not in the template. In this approach prevai such concepts as FormControl, FormGroup, FormArray, FormBuilder.

FormControl is produced automatically or manually for each form of an element.

FormGroup assists in grouping different controls and it is accommodative.

FormArray is used when a form has a list in which you can add items.

FormBuilder assists in simplifying the operation of making new forms. We can insert it into the controller.

To attach a validator to the control in the reactive approach, you have to affix another parameter, which can be both validator and array. You import an assemblage of validators (min, max, required, requiredTrue, email, minLength, maxLength, pattern). You can specify not only one, but a few validators.

To force validators and parameters switch the validator function into another function, which will make a conclusion for storing the parameters.

Also there is the concept of asynchronous validators. They differ from the synchronous validators by the possibility of returning non-statistical data.

Igor Grigorenko

Add comment