Challenge - 5 Problems
Validation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
How does Angular show validation errors in a reactive form?
Consider a reactive form with a required input field. What will Angular display when the user focuses out of the input without entering any value?
Angular
import { Component } from '@angular/core'; import { FormControl, Validators } from '@angular/forms'; @Component({ selector: 'app-sample-form', template: ` <input [formControl]="nameControl" aria-label="Name input" /> <div *ngIf="nameControl.invalid && (nameControl.dirty || nameControl.touched)" role="alert"> Name is required. </div> `, standalone: true }) export class SampleFormComponent { nameControl = new FormControl('', Validators.required); }
Attempts:
2 left
💡 Hint
Think about when Angular marks a control as touched or dirty to show errors.
✗ Incorrect
Angular shows validation errors when the control is invalid and either touched or dirty. This means the user interacted with the input and then left it empty, triggering the error message.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this Angular template for showing validation errors
Which option correctly fixes the syntax error in this Angular template snippet?
Template snippet:
`
Error
`Angular
<input [formControl]="formControl" /> <div *ngIf="formControl.invalid && formControl.touched">Error</div>
Attempts:
2 left
💡 Hint
Check if the *ngIf directive syntax is correct and if the condition is valid.
✗ Incorrect
The *ngIf directive syntax is correct and the condition uses valid properties of the form control. No syntax error exists.
🔧 Debug
advanced2:00remaining
Why does the validation error not show even when the input is empty and touched?
Given this Angular reactive form code, why does the error message not appear when the input is empty and the user has focused and blurred the input?
Component template:
`
Email is required.
`
Component code:
`emailControl = new FormControl('', []);`Attempts:
2 left
💡 Hint
Check the validators assigned to the FormControl.
✗ Incorrect
The FormControl has an empty validators array, so it never becomes invalid. Without a required validator, the control is always valid even if empty.
❓ state_output
advanced2:00remaining
What is the value of formControl.errors after invalid input?
If a FormControl has Validators.required and Validators.minLength(5), and the user enters 'abc', what is the value of formControl.errors?
Angular
import { FormControl, Validators } from '@angular/forms'; const control = new FormControl('abc', [Validators.required, Validators.minLength(5)]); const errors = control.errors;
Attempts:
2 left
💡 Hint
Check which validators fail given the input 'abc'.
✗ Incorrect
The input is not empty, so required passes. But length is less than 5, so minlength error appears with details.
🧠 Conceptual
expert2:00remaining
Which Angular feature helps show validation errors only after user interaction?
In Angular reactive forms, which feature ensures validation errors appear only after the user has interacted with the form control?
Attempts:
2 left
💡 Hint
Think about how Angular tracks user interaction with form controls.
✗ Incorrect
The touched and dirty properties indicate if the user has focused or changed the input, so errors show only after interaction.