0
0
Angularframework~20 mins

Form state tracking (dirty, touched, valid) in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Form State Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the form's valid state after user interaction?

Consider an Angular standalone form with a required input field. The user focuses the input, types a valid value, then blurs the input.

What will be the valid state of the form after these actions?

Angular
import { Component } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-simple-form',
  standalone: true,
  template: `
    <form [formGroup]="form">
      <input formControlName="name" />
    </form>
  `
})
export class SimpleFormComponent {
  form = new FormGroup({
    name: new FormControl('', Validators.required)
  });
}
AThe form is invalid because the input is dirty but not touched.
BThe form is invalid because the input is still marked as untouched.
CThe form is valid because the input has a non-empty value that meets the required validator.
DThe form is valid only if the input is touched and dirty.
Attempts:
2 left
💡 Hint

Remember that valid depends on validators, not on touched or dirty states.

state_output
intermediate
2:00remaining
What is the dirty state of the form after initialization?

Given an Angular reactive form initialized with default values, what is the dirty state of the form immediately after creation?

Angular
import { Component } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';

@Component({
  selector: 'app-init-form',
  standalone: true,
  template: `<form [formGroup]="form"></form>`
})
export class InitFormComponent {
  form = new FormGroup({
    email: new FormControl('user@example.com')
  });
}
AThe form is pristine because no user changes have been made yet.
BThe form is invalid because the control has a value but no validators.
CThe form is touched because the control has a value.
DThe form is dirty because it has initial values.
Attempts:
2 left
💡 Hint

Think about what dirty means: user changed the value or not.

🔧 Debug
advanced
2:00remaining
Why does the form's touched state remain false after input blur?

In this Angular form, the input loses focus but the form's touched state remains false. What is the most likely cause?

Angular
import { Component } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-debug-form',
  standalone: true,
  template: `
    <form [formGroup]="form">
      <input />
    </form>
  `
})
export class DebugFormComponent {
  form = new FormGroup({
    username: new FormControl('', Validators.required)
  });
}
AThe input element is missing the <code>formControlName</code> directive, so Angular can't track touch events.
BThe form control is disabled, so touch events are ignored.
CThe input element is not inside the <code>form</code> tag, so touch events are not registered.
DThe user did not actually focus and blur the input, so touched remains false.
Attempts:
2 left
💡 Hint

Check if Angular can connect the input to the form control properly.

🧠 Conceptual
advanced
2:00remaining
How does Angular determine a form control is dirty?

Which of the following best describes when Angular marks a form control as dirty?

AWhen the control's value is programmatically set using <code>setValue</code>.
BWhen the control's value changes from its initial value due to user interaction.
CWhen the control is blurred after being focused.
DWhen the control receives focus for the first time.
Attempts:
2 left
💡 Hint

Think about what user action changes the control's state.

📝 Syntax
expert
2:30remaining
Which code snippet correctly checks if a form is valid and dirty in Angular 17+ standalone component?

Choose the correct Angular code to conditionally enable a submit button only if the form is valid and dirty.

Angular
import { Component } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-submit-form',
  standalone: true,
  template: `
    <form [formGroup]="form">
      <input formControlName="email" />
      <button [disabled]="???">Submit</button>
    </form>
  `
})
export class SubmitFormComponent {
  form = new FormGroup({
    email: new FormControl('', [Validators.required, Validators.email])
  });
}
A[disabled]="!form.valid && !form.dirty"
B[disabled]="form.valid && form.dirty"
C[disabled]="!form.valid || form.pristine"
D[disabled]="form.invalid || !form.dirty"
Attempts:
2 left
💡 Hint

The button should be disabled if the form is invalid or not dirty.