0
0
Angularframework~20 mins

Showing validation errors in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Validation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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);
}
AThe error message 'Name is required.' appears only after the input loses focus without a value.
BThe error message appears immediately when the form loads, before user interaction.
CNo error message appears because the input is empty but untouched.
DThe error message appears only if the user submits the form.
Attempts:
2 left
💡 Hint
Think about when Angular marks a control as touched or dirty to show errors.
📝 Syntax
intermediate
2: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>
AReplace formControl.touched with formControl.dirty to show error only when input changed.
BChange *ngIf to [hidden] to hide the div instead of removing it.
CNo syntax error; the code is correct as is.
DAdd parentheses around the condition: *ngIf="(formControl.invalid && formControl.touched)"
Attempts:
2 left
💡 Hint
Check if the *ngIf directive syntax is correct and if the condition is valid.
🔧 Debug
advanced
2: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('', []);`
AThe *ngIf condition is incorrect; it should check emailControl.dirty instead of touched.
BThe FormControl has no validators, so it is never invalid.
CThe input is missing the required attribute, so validation does not trigger.
DThe error message is hidden by CSS, so it does not appear.
Attempts:
2 left
💡 Hint
Check the validators assigned to the FormControl.
state_output
advanced
2: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;
A{"minlength": {"requiredLength": 5, "actualLength": 3}}
B{"required": true}
Cnull
D{"minlength": true, "required": true}
Attempts:
2 left
💡 Hint
Check which validators fail given the input 'abc'.
🧠 Conceptual
expert
2: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?
AAdding the novalidate attribute to the form element.
BSetting the updateOn option to 'submit' when creating the FormControl.
CUsing the Validators.compose method to combine validators.
DUsing the touched and dirty properties of the FormControl in the template condition.
Attempts:
2 left
💡 Hint
Think about how Angular tracks user interaction with form controls.