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
**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
807
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.
808
+
-**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:
809
+
1. Call `event.preventDefault()` to prevent the default form submission.
810
+
2. Use the `submit(this.myForm, ...)` utility.
811
+
3. Update the `RecipeService` to include an `addRecipe(newRecipe: RecipeModel)` method.
812
+
4. Construct a complete `RecipeModel` (merging form values with defaults) and pass it to the service.
813
+
5.**Reset the form**: Call `this.myForm().reset()` to clear interaction flags.
814
+
6.**Clear the values**: Call `this.myModel.set(...)` to reset the inputs.
813
815
814
816
-**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
817
-`name`: Required (Message: 'Recipe name is required.').
816
818
-`description`: Required (Message: 'Description is required.').
817
819
-`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.**
820
+
**Finally, ensure your submit button has `type="submit"` and bind its`[disabled]` property to `myForm().invalid()` so users cannot submit invalid data.**
819
821
820
822
-**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
823
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()`.
824
+
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
825
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