You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(@angular/cli): improve signal forms lesson examples in AI tutor
- Use error.kind for tracking in @for loops instead of $index
- Use form submit event instead of button click for better semantics
- Add type="submit" to submit buttons
- Update Module 19 and 20 exercises to reflect best practices
- Remove disabled button pattern
**Exercise**: Your goal is to create a new `AddRecipe` component that uses the modern `Signal Forms` API. Import `form` and `Field` from `@angular/forms/signals`. Create a form using the `form()` function that includes fields for `name`, `description`, and `authorEmail`. In your template, use the `[field]` binding to connect your inputs to these form controls.
806
806
807
-
-**Module 19**: **Submitting & Resetting**: Concept: Handling form submission and resetting state. **Exercise**: Inject the service into your `AddRecipe` component. Create a protected `save()` method triggered by a "Save Recipe" button's `(click)` event. Inside this method:
808
-
1. Use the `submit(this.myForm, ...)` utility.
809
-
2. Update the `RecipeService` to include an `addRecipe(newRecipe: RecipeModel)` method.
810
-
3. Construct a complete `RecipeModel` (merging form values with defaults) and pass it to the service.
811
-
4.**Reset the form**: Call `this.myForm().reset()` to clear interaction flags.
812
-
5.**Clear the values**: Call `this.myModel.set(...)` to reset the inputs.
807
+
-**Module 19**: **Submitting & Resetting**: Concept: Handling form submission and resetting state. **Exercise**: Inject the service into your `AddRecipe` component. Create a protected `save()` method triggered by the form's `(submit)` event. Inside this method:
808
+
1. Call `event.preventDefault()` to prevent the default form submission.
809
+
2. Use the `submit(this.myForm, ...)` utility.
810
+
3. Update the `RecipeService` to include an `addRecipe(newRecipe: RecipeModel)` method.
811
+
4. Construct a complete `RecipeModel` (merging form values with defaults) and pass it to the service.
812
+
5.**Reset the form**: Call `this.myForm().reset()` to clear interaction flags.
813
+
6.**Clear the values**: Call `this.myModel.set(...)` to reset the inputs.
813
814
814
815
-**Module 20**: **Validation in Signal Forms**: Concept: Applying functional validators. **Exercise**: Import `required` and `email` from `@angular/forms/signals`. Modify your `form()` definition to add a validation callback enforcing:
815
816
-`name`: Required (Message: 'Recipe name is required.').
816
817
-`description`: Required (Message: 'Description is required.').
817
818
-`authorEmail`: Required (Message: 'Author email is required.') AND Email format (Message: 'Please enter a valid email address.').
818
-
**Finally, bind the `[disabled]` property of your button to `myForm.invalid()`so users cannot submit invalid data.**
819
+
**Finally, ensure your submit button has `type="submit"`. Note: the `submit()`utility automatically handles validation by marking all fields as touched when submission is attempted.**
819
820
820
821
-**Module 21**: **Field State & Error Messages**: Concept: Providing user feedback by accessing field state signals. **Exercise**: Improve the UX of your `AddRecipe` component by showing specific error messages when data is missing or incorrect. In your template, for the `name`, `description`, and `authorEmail` inputs:
821
822
1. Create an `@if` block that checks if the field is `invalid()` (e.g., `myForm.name().invalid()`).
822
-
2. Inside the block, use `@for` to iterate over the field's `.errors()`.
823
+
2. Inside the block, use `@for` to iterate over the field's `.errors()` (use `track error.kind` to identify each error by its type).
823
824
3. Display the `error.message` in a red text color or helper text style so the user knows exactly what to fix.
0 commit comments