
In this article, we shall focus on, how to create a custom validator and assign it to a FormControl. Angular provides us with few in-built validators which can be used for validation. But there may be some scenarios in which we need to give custom validation to our control. In such a case, we create a custom validator.
Let's start with an example program. First, create an angular application using
ng new angular-app
Then enter into the project using
cd angular-app
To use reactive forms, we need to import ReactiveFormsModule in ourapp.module.ts file. Let's modify the code in our app.module.ts to import theReactiveFormsModule and add it to imports array to make use of it.
Now, let's create a new component named, AddEmployeeComponent.
ng g c add-employee
Let's modify the app-employee.component.ts file
We are about to create a simple form to get employee details such as their name, age and gender. To make use of reactive forms we have to import FormBuilder, FormGroup and Validators. Next, we need to inject theFormBuilder to the constructor with a name 'fb'.
In ngOnInit() life cycle event, we create a FormGroup namedemployeeForm and assign a formGroup by creating formGroup with FormBuilder object 'fb'. The FormGroup contains 3 formControl namedname, age and gender. The name and gender FormControls are assigned a default value of empty string and apply single 'required' validation. Similarly, for age FormControl, we assign a default value of empty string and to this control we are going to apply multiple validation to this control, so we use array notation for it. So the code looks something like below
Let's create a custom validator and assign it to the age FormControl validation array. Create a folder named custom-validator and create a file named age-validator.ts and modify the code as below.
This custom validator is used to set validation when the given age is greater than 80. The FormControl value is obtained from the parameter ofageValidator() function. From the parameter, we get the value and check whether the age is greater than 80. If so, we set a validation with the name'ageInValid' to true and return it. If not, we return null to ensure that it passes the validation check. Now we are going to add the validation to the add-employee.component.ts component by modifying its code as below.
below.
In add-employee.component.html file, we modify the code as below
In the forms tag, we apply the FormGroup name which is 'employeeForm'using the syntax [formGroup]='employeeForm' and to connect FormControl to html input element using formControlName property for the name, age and gender. To get the control, we use get() method along with the name of the FormControl name as parameter from which we can using validproperty we can check whether the control is valid or not and using touchedproperty we can check whether the control has been touched or not. To get the errors set to the control we use errors property from which we can get an object with error parameters set to true. To show up validation, we check whether the control is valid or not and also check touched property to show up validation. To show up custom validation we use the syntax in ngFordirective.
!employeeForm.get('age').valid && employeeForm.get('age').touched && employeeForm.get('age').errors.ageInValidIf the age entered is greater than 80, then the custom validator will set ageInValid to true. Now you can apply the selector string app-add-employeeto the app.component.ts to make use of it.
Comments
Post a Comment