In this step by step Ionic 6 Forms validation tutorial, we will learn to create and validate a form with Angular’s Reactive Forms method.
Setting up forms in an Ionic application is easy, Angular 9|10 offers Template-driven and Reactive Forms methods to deal with the forms data.
Template-driven approach is used for working with simple forms. In contrast, It is used for handling more complex data. Especially when you need to work with nested values.We will create a basic form with following input fields name, email, dob and mobile number.
Define Form Structure with Ionic HTMLSet Up Ionic Project
Ionic CLI has to be installed on the development machine, and you can use the following command to install or update the tool:
To setup a form in an Angular/Ionic app, you need to run the below command.
Get inside the project directory.
Install lab mode as a dev-dependency by running the following command.
Start the Ionic app.
To set up a form in Ionic use Ionic form UI components, below we are creating a form for school students and a student can fill information about himself using the form. We used Ionic’s checkboxes, radio buttons, buttons and other elements to build this form.
Open home.page.html
file and add the following code inside of it.
Import & Register ReactiveFormsModule
To work with Reactive Forms we need to import and register ReactiveFormsModule
. Now usually when we work with core Angular app so we declare ReactiveFormsModule globally in app.module.ts file. However, we have to declare ReactiveFormsModule in every page or component when we work with forms module.
Open home.module.ts
and add the below code in it.
Creating Form in Ionic with Reactive Forms
Next, we will use FormControl, FormGroup, FormBuilder, and Validators service to validate the form data.
Let’s understand what these services are?
FormGroup: A FormGroup is a collection of single or multiple FormControls and declared on the HTML’s form tag. Basically, its a collection of various form controls.
FormControl: A FormControl represents a value which is entered by the user, FormGroup is a collection of various FormControls.
FormBuilder: The FormBuilder service refers to a form object and sets up a FormGroup. It holds the user entered values and validation state of a form input field.
Import the following services to initialize the form in home.page.ts
.
Define the form object by declaring the FormGroup with a variable.
Inject the FormBuilder service in the constructor.
Setting up Date-picker in Ionic
Date-picker is used to get the date from the user, Ionic offers its own custom date-picker to deal with the date related date. We declared the date with ion-datetime tag in the HTML template. Now we will get the date value from the user and set in the dob formControl in Angular’s Reactive form.
We defined the defaultDate variable to set the default date. The getDate(e)
function takes date object as an argument and convert it to YYYY-MM-DD format and set as a FormControl in the FormGroup.
The (ngSubmit)="submitForm()"
directive triggers a form submission.
Enter some values in the form field and click on submit button, you will see user entered values in the ionicForm
in the browser’s console.
Validate Ionic Form
In this section we will learn how to validate Ionic form fields using Reactive Forms validators service.
Import Validators
service to in the Ionic template, It provides built-in methods to validate form controls.
The Validators class offers following methods to deal with form validation in Ionic / Angular.
We will trigger the validation when user clicks on the submit button, so we need to define a boolean variable and set it to false initially.
Now, we will validate our name form control. In this value we will implement required and min-length validation.
As you can see we injected the validation methods with the help of Validators class. Validators array allow us to implement multiple validation in a single form control.
Now, we will declare the getter method to access the form control. Add the following code to access the form control directly via the Ionic / Angular template.
Next, we need to add the alert messages in the Ionic’s HTML template for showing the validation messages. These, messages will appear when the form is invalid.
Inside the *ngIf directive, we are checking if both the conditions are true. The *ngIf=""
directive will only show the alert message to the users when the form is invalid.
Following method will trigger the form on submit.
Here are the final code of this Ionic Angular Form validation tutorial.
home.page.ts
home.page.html
Conclusion
Finally, we have finished Ionic Angular Form Validation tutorial using reactive forms.
We just learned the basic form validation with Reactive Forms in Ionic app, I hope you will surely liked it.