How To Create Form In Angular
The data is collected from the user using Angular forms. In this article, we will learn about what is Angular Forms, and then we will examine how to use Angular Forms with sample examples in the following article.
Data entry forms can range from simple to sophisticated. It can have a big number of input fields that span numerous tabs. Complex validation logic dependent on many fields may also be present in forms.
There are some things that forms are required to do.
- Set up the form fields and show them to the user.
- Obtain information from the user.
- Keep track of the modifications you've made to the fields.
- Validate the data.
- Show the user helpful errors.
Angular Forms Module
All of the above services are available out of the box with the Angular forms module. It connects the Angular component class to the form field. It keeps track of changes to the form fields so we can respond appropriately. Validators are built-in to Angular forms to ensure that inputs are correct. You can make your own custom validator from scratch. It shows the user the validation errors. When the user submits the form, it encapsulates all of the input fields into an object structure.
The forms are built in two ways by Angular. One way is template-driven forms, while the other is reactive forms or model-driven forms.
Template-driven forms approach
The simplest method to create Angular forms is to use a template-driven approach. The form's logic is stored in the template. This is a similar approach to what we used in AngularJs.
Model-driven forms approach
The logic of the form is defined in the component as an object in the Reactive Forms or Model-driven method. The model-driven method has greater advantages because it simplifies component testing.
The form's representation is built in the component class in this technique. The HTML elements are then bound to this form model. It's done with the use of specific markups.
With Agular template-driven forms, we can easily design sophisticated-looking forms without having to write any javascript code. Form fields are produced as properties of our component class, and model-driven forms are created in the component class. This makes testing a lot easier.
Building Blocks of Angular Forms
Regardless of whether you're use Template-driven or Reactive forms, the Angular Forms module is made up of three building components.
FormControl
In an Angular form, FormControl represents a single input field.
Take a look at a simple Text input box.
First Name : <input type="text" name="firstname" />
let firstname= new FormControl(); //Creating a FormControl in a Reactive forms
The value property can then be used to access the current value in the input field.
firstname.value //Returns the value of the first name field
The validity status of the First Name element can be checked as shown below.
firstname.errors // returns the list of errors firstname.dirty // true if the value has changed (dirty) firstname.touched // true if input field is touched firstname.valid // true if the input value has passed all the validation
FormGroup
city : <input type="text" name="city" > Street : <input type="text" name="street" > PinCode : <input type="text" name="pincode" >
let address= new FormGroup({ street : new FormControl(""), city : new FormControl(""), pinCode : new FormControl("") })
address.value
The Return value
address { street :"", city:"", Pincode:"" }
you can access to child control as.
address.get("street")
Check the status of validation as follows:
address.errors // returns the list of errors address.dirty // true if the value of one of the child control has changed (dirty) address.touched // true if one of the child control is touched address.valid // true if all the child controls passed the validation
FormArray
contactForm = new FormGroup( { name: new FormControl(''), cities:new FormArray([ new FormControl('Mumbai'), new FormControl('Delhi') ]) });
The contactForm can provide you with the city references. method to obtain
cities() :FormArray { return this.contactForm.get("cities") as FormArray }
Check the status of validation as follows:
cities.errors // returns the list of errors cities.dirty // true if the value of one of the child control has changed (dirty) cities.touched // true if one of the child control is touched cities.valid // true if all the child controls passed the validation