Now that you're able to validate the form, it's important to show validation errors to users.
In this activity, you'll learn how to:
- Access field state with validation signals
- Use
@ifto conditionally display errors - Loop through errors with
@for - Show errors only after user interaction
Let's display validation feedback!
-
Add error display for email field
Below the email input, add conditional error display. This will only show errors when the field is both invalid and touched:
<label> Email <input type="email" [field]="loginForm.email" /></label>@if (loginForm.email().invalid() && loginForm.email().touched()) { <div class="error"> @for (error of loginForm.email().errors(); track error.kind) { <span>{{ error.message }}</span> } </div>}The
loginForm.email()call accesses the field's state signal. Theinvalid()method returnstruewhen validation fails,touched()returnstrueafter the user has interacted with the field, anderrors()provides an array of validation errors with their custom messages. -
Add error display for password field
Below the password input, add the same pattern for password errors:
Excellent! You've added error display to your form. The errors appear only after users interact with a field, providing helpful feedback without being intrusive.
Next, you'll learn how to handle form submission!