Bird
0
0

You want to create a form that requires a user to enter a non-empty email and password. Which Angular approach best ensures the form is valid only when both fields are filled?

hard🚀 Application Q15 of 15
Angular - Template-Driven Forms
You want to create a form that requires a user to enter a non-empty email and password. Which Angular approach best ensures the form is valid only when both fields are filled?
AUse only FormControl for email and ignore password validation.
BUse FormGroup with FormControls and add Validators.required to each control.
CUse FormGroup without validators and check values manually after submit.
DUse FormGroup with empty FormControls and no validation.
Step-by-Step Solution
Solution:
  1. Step 1: Understand Angular validation

    Validators.required ensures a control is not empty and marks form invalid if empty.
  2. Step 2: Apply validators to both email and password controls

    Adding Validators.required to both controls enforces non-empty input before form is valid.
  3. Step 3: Compare other options

    Manual checks or missing validators do not enforce form validity automatically.
  4. Final Answer:

    Use FormGroup with FormControls and add Validators.required to each control. -> Option B
  5. Quick Check:

    Validators.required on controls = form valid only if filled [OK]
Quick Trick: Add Validators.required to controls for mandatory fields [OK]
Common Mistakes:
MISTAKES
  • Skipping validators and relying on manual checks
  • Validating only one field
  • Assuming empty controls are valid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes