0
0
Angularframework~20 mins

FormBuilder service in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FormBuilder Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Angular form control value?
Given this Angular component code using FormBuilder, what will be the value of this.myForm.value after initialization?
Angular
import { Component, inject } from '@angular/core';
import { FormBuilder } from '@angular/forms';

@Component({
  selector: 'app-sample',
  template: ``,
  standalone: true
})
export class SampleComponent {
  fb = inject(FormBuilder);
  myForm = this.fb.group({
    firstName: ['John'],
    lastName: ['Doe']
  });
}
A{"firstName": null, "lastName": null}
B{"firstName": ["John"], "lastName": ["Doe"]}
C{"firstName": "John", "lastName": "Doe"}
D{}
Attempts:
2 left
💡 Hint
Remember that FormBuilder initializes controls with the values inside the array.
📝 Syntax
intermediate
2:00remaining
Which option correctly creates a FormGroup with a required validator on 'email' using FormBuilder?
Select the code snippet that correctly uses FormBuilder to create a form group with an 'email' control that has a required validator.
Athis.fb.group({ email: ['', [Validators.required]] })
Bthis.fb.group({ email: ['', Validators.required] })
Cthis.fb.group({ email: ['', Validators.required()] })
Dthis.fb.group({ email: ['', Validators.required] }, Validators.email)
Attempts:
2 left
💡 Hint
Validators should be passed as an array when multiple or single validators are used.
state_output
advanced
2:00remaining
What is the value of 'username' control after patchValue call?
Consider this Angular component snippet. What will be the value of the 'username' control after this.myForm.patchValue({ username: 'alice' }) is called?
Angular
import { Component, inject } from '@angular/core';
import { FormBuilder } from '@angular/forms';

@Component({
  selector: 'app-user',
  template: ``,
  standalone: true
})
export class UserComponent {
  fb = inject(FormBuilder);
  myForm = this.fb.group({
    username: ['bob'],
    age: [30]
  });

  updateUsername() {
    this.myForm.patchValue({ username: 'alice' });
  }
}
A"alice"
B"bob"
Cnull
Dundefined
Attempts:
2 left
💡 Hint
patchValue updates only specified controls without affecting others.
🔧 Debug
advanced
2:00remaining
Why does this FormBuilder code cause a runtime error?
Examine the following code snippet. Why will it cause a runtime error when the component initializes?
Angular
import { Component, inject } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';

@Component({
  selector: 'app-login',
  template: ``,
  standalone: true
})
export class LoginComponent {
  fb = inject(FormBuilder);
  loginForm = this.fb.group({
    email: ['', Validators.required, Validators.email]
  });
}
AFormBuilder group method requires controls to be strings, not arrays.
BValidators must be passed as an array, not as separate arguments.
CThe FormBuilder instance is not injected properly.
DValidators.required and Validators.email cannot be used together.
Attempts:
2 left
💡 Hint
Check how validators are passed in FormBuilder control arrays.
🧠 Conceptual
expert
3:00remaining
What happens if you use FormBuilder to create nested FormGroups incorrectly?
Consider this code snippet. What error or behavior will occur when trying to access this.profileForm.get('address.street')?
Angular
import { Component, inject } from '@angular/core';
import { FormBuilder } from '@angular/forms';

@Component({
  selector: 'app-profile',
  template: ``,
  standalone: true
})
export class ProfileComponent {
  fb = inject(FormBuilder);
  profileForm = this.fb.group({
    name: [''],
    address: this.fb.control({
      street: [''],
      city: ['']
    })
  });
}
AIt will return undefined because 'address' is not defined.
BIt will return the FormControl for 'street' correctly.
CIt will throw a runtime error due to invalid FormBuilder usage.
DIt will return null because 'address' is a FormControl, not a FormGroup.
Attempts:
2 left
💡 Hint
Check the difference between FormControl and FormGroup in FormBuilder.