Angular Validations Q&As
Angular Validations Q&As
By
www.questpond.com
Contents
1. What are two ways of doing validation in Angular? .................................................................... 1
2. Differentiate between Template driven forms VS Reactive Forms? ............................................ 2
3. In what situations you will use what? .......................................................................................... 2
4. Explain template reference variables ? ........................................................................................ 2
5. How do we implement Template driven forms?.......................................................................... 2
6. How do we implement Reactive forms ? ..................................................................................... 3
7. How can we implement composite validations? ......................................................................... 4
8. How can we implement dynamic validations? ............................................................................ 5
9. How to check if overall validation and specific validations are good ? ........................................ 5
10. Can you talk about some inbuilt validators? ................................................................................ 6
11. How can you create your own custom validator ? ....................................................................... 6
12. Can Angular validations work with out FORM tag? ..................................................................... 6
13. What is [ngModelOptions]="{standalone: true}”? ...................................................................... 7
In template driven forms you write validation inside the template (Declarative) while in reactive forms
you can write the validation programmatically (Imperative) in the component.
Simplicity Declarative, Simple to write. Complex to write but you have more control.
Complex scenarios Template code becomes complex. Can be handled nicely in Component.
Dynamic validation Very difficult. Can be easily handled.
Unit testing Difficult due to Async nature. Can be tested easily.
My personal view: - I would always prefer reactive forms because for some reason if the complexity of the
validation increases, porting from template drive to reactive is again a big work. Also by using same way of
implementation we have uniformity.
Template variables help you use access DOM elements inside the Angular template. You can see the
code below we have create “mytext” variable and we are accessing it inside the angular expression.
Reactive form validation is done in typescript code as shown below. We will need to use the
“FormBuilder” to create the “FormGroup” and inside that we can define array of “Control” and
validations.
Once you have created the validations in the back end you need to apply on the front end by using
“formGroup” and “formControlName”. To check if validations are ok we can use “valid method” as
shown in below. For specific validation you need to use “hasError”.
You can put the validation as collection as shown in the below code snippet.
this.skillsForm = this.fb.group({
name: ['',[Validators.required , Validators.maxLength(20)]],
skills: this.fb.array([]) ,
});
Step 3 :- Once dynamic validation is created you can apply it dynamically using the angular “forloop”
<div formArrayName="skills">
<div *ngFor="let skill of skills.controls; let i=index">
<div [formGroupName]="i">
<input type="text" formControlName="skillName">
<div [hidden]="!(skill.get('skillName')?.hasError('required'))">
Skill Name required
</div>
</div>
</div>
To check if all validations of the “FormGroup” are good we can use the “valid” property.
To check if specific validation and specific control is valid we will need to use “hasError” method as
shown in the below code snippet where “name” is the control and we are checking if “required”
validator is valid or not.
<div *ngIf="myForm.controls['name'].touched
&& myForm.controls['name'].hasError('required')">
Name is required
</div>
Some inbuilt validators which you can remember talk during interview. You do not have to remember all
pick some and speak.
You need to implement “ValidatorFn” which is present in “@angular/forms”. You can write your custom
code and return null if all validations are good or you return true.
if (passwordRegex.test(control.value)) {
return null; // passes the test, return null to clear any errors
}
// fails the test, therefore the password is of invalid structure
return { invalidPassword: true };
};
};
If you have a control which is inside the “formGroup” and it has “ngModel” applied as shown in the
below figure ( second textbox “test1”) then a “formControl” is created by default and applied to that
control. This leads to an error as we do not have validation applied to it.
So one way to handle is by removing the textbox outside the “form” tag. Now this solution can disturb
the designing or structure.
The more clean way is to specify “standalone:true” which says that do not create a “formControl” on this
control.