0
0
Angularframework~20 mins

ReactiveFormsModule setup in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Reactive Forms Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when submitting a form with empty required fields?

Consider an Angular component using ReactiveFormsModule with a form control marked as Validators.required. What will be the form's valid status and error state after submitting the form without entering any value?

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

@Component({
  selector: 'app-simple-form',
  template: `
    <form (ngSubmit)="onSubmit()">
      <input [formControl]="nameControl" placeholder="Name" />
      <button type="submit">Submit</button>
    </form>
    <p *ngIf="nameControl.invalid && nameControl.touched">Name is required.</p>
  `
})
export class SimpleFormComponent {
  nameControl = new FormControl('', Validators.required);

  onSubmit() {
    console.log('Form valid:', this.nameControl.valid);
    console.log('Errors:', this.nameControl.errors);
  }
}
AForm valid: true; Errors: { required: true }
BForm valid: true; Errors: null
CForm valid: false; Errors: null
DForm valid: false; Errors: { required: true }
Attempts:
2 left
💡 Hint

Think about what Validators.required does when the input is empty.

📝 Syntax
intermediate
2:00remaining
Which option correctly imports ReactiveFormsModule in an Angular standalone component?

You want to use reactive forms in a standalone Angular component. Which import statement and component decorator setup is correct?

A
import { ReactiveFormsModule } from '@angular/forms';

@Component({
  standalone: true,
  imports: [ReactiveFormsModule],
  selector: 'app-standalone',
  template: `&lt;form&gt;&lt;/form&gt;`
})
export class StandaloneComponent {}
B
import { FormsModule } from '@angular/forms';

@Component({
  standalone: true,
  imports: [FormsModule],
  selector: 'app-standalone',
  template: `&lt;form&gt;&lt;/form&gt;`
})
export class StandaloneComponent {}
C
import { ReactiveFormsModule } from '@angular/forms';

@NgModule({
  imports: [ReactiveFormsModule],
  declarations: [StandaloneComponent]
})
export class StandaloneModule {}

@Component({
  selector: 'app-standalone',
  template: `&lt;form&gt;&lt;/form&gt;`
})
export class StandaloneComponent {}
D
import { ReactiveFormsModule } from '@angular/forms';

@Component({
  selector: 'app-standalone',
  template: `&lt;form&gt;&lt;/form&gt;`
})
export class StandaloneComponent {}
Attempts:
2 left
💡 Hint

Standalone components require explicit imports of modules they use.

state_output
advanced
2:00remaining
What is the value of the form control after patchValue with partial data?

Given a form group with two controls, what will be the value of the form after calling patchValue with only one control's value?

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

@Component({
  selector: 'app-patch-form',
  template: `<p>Check console for output</p>`
})
export class PatchFormComponent {
  form = new FormGroup({
    firstName: new FormControl('John'),
    lastName: new FormControl('Doe')
  });

  constructor() {
    this.form.patchValue({ firstName: 'Jane' });
    console.log(this.form.value);
  }
}
A{ firstName: 'Jane' }
B{ lastName: 'Doe' }
C{ firstName: 'Jane', lastName: 'Doe' }
D{}
Attempts:
2 left
💡 Hint

Remember what patchValue does compared to setValue.

🔧 Debug
advanced
2:00remaining
Why does this form control's value not update on input?

In this Angular component using reactive forms, the input field does not update the form control's value when typing. What is the cause?

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

@Component({
  selector: 'app-input-debug',
  template: `<input [formControl]="nameControl" />`
})
export class InputDebugComponent {
  nameControl = new FormControl('');

  constructor() {
    this.nameControl.setValue('Initial');
  }
}
AThe input uses [formControl] correctly; the issue is elsewhere.
BThe form control is set programmatically in constructor, which blocks user input updates.
CThe input is missing the 'ngModel' directive for two-way binding.
DThe input is missing the 'formControlName' directive instead of 'formControl'.
Attempts:
2 left
💡 Hint

Check if the binding syntax matches reactive forms usage.

🧠 Conceptual
expert
2:00remaining
What happens if you forget to import ReactiveFormsModule in your Angular module?

You create a reactive form in a component but forget to import ReactiveFormsModule in your Angular module. What error or behavior will you observe when running the app?

AThe form works normally without errors because ReactiveFormsModule is optional.
BAngular throws a template parse error: 'formControl' is not a known property of 'input'.
CThe app compiles but the form controls do not update their values on input.
DAngular throws a runtime error about missing providers for FormControl.
Attempts:
2 left
💡 Hint

Think about what Angular needs to recognize formControl in templates.