Skip to content

Commit b754e93

Browse files
committed
Update Form chapter practical work
1 parent 782efcd commit b754e93

File tree

2 files changed

+56
-21
lines changed

2 files changed

+56
-21
lines changed

docs/src/forms/README.md

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ The following component shows an example of how to take advantage of:
136136
You can find an updated list of classes [here](https://angular.io/guide/form-validation#control-status-css-classes).
137137

138138
## Practical work: Login and registration with reactive forms
139-
1. Implement the login / registration form using reactive forms and the form builder: replace the `[(ngModel)]` in the template and delete the `email` and `password` from the class of the `LoginFormComponent`.
139+
1. Implement the login / registration form using reactive forms and the form builder: replace the `[(ngModel)]` in the template and delete the `email` and `password` from the class of the `LoginFormComponent`, declare a `FormGroup` named `loginForm`.
140140

141141
2. Add the required validator (`Validators.required`) to both email and password fields. Show the text `"This field is required"` under each field if the form is dirty and that field has the error `required`:
142142

@@ -145,29 +145,45 @@ You can find an updated list of classes [here](https://angular.io/guide/form-val
145145
```
146146

147147
::: tip Hint
148-
- You can get a specific form control using the `get('form control name')` method of a form group.
149-
- You can check if a form control has a specific error by using the `hasError('error name')` method of form controls.
148+
- You can get access to a specific `FormControl` instance using the `controls` property of the `FormGroup` containing it. For exemple for a `FormGroup` named *myFormGroup* and a `FormControl` named *myControl*:
149+
```ts
150+
myFormGroup.controls.myControl.touched
151+
```
152+
- You can check if a `FormControl` (more generally an `AbstractControl`) has a specific error by using the `hasError('error name')` method of the `FormControl` class. For exemple for a `FormGroup` named *myFormGroup*, a `FormControl` named *myControl* and an error *required*:
153+
```ts
154+
myFormGroup.controls.myControl.hasError('required')
155+
```
150156
:::
151157

152-
3. Style the label and error text of each field with the `.ng-invalid` class when the form is dirty and that field is invalid (remember the `[class]` attribute). Be careful to target only those two elements (you can use the `+` CSS selector to that purpose).
158+
3. Style the label and error text of each input that has the `.ng-invalid` class applied by angular but only when the form has the `ng-dirty` class. Be careful to target only those two elements (you can use the `+` CSS combinator and the `:has()` pseudo-class to that purpose).
153159

154160
::: details Hint
155-
```scss
156-
label.ng-invalid, input.ng-invalid.ng-dirty + small {
161+
- [has() pseudo class](https://developer.mozilla.org/en-US/docs/Web/CSS/:has)
162+
- [+ next-sibling combinator](https://developer.mozilla.org/en-US/docs/Web/CSS/Next-sibling_combinator)
163+
:::
164+
165+
::: details Correction
166+
```css
167+
form.ng-dirty label:has(+ input.ng-invalid), form.ng-dirty input.ng-invalid + small {
157168
color: red;
158169
}
159170
```
160171
:::
161172

162-
4. Disable the Login and Register buttons using the `[disabled]` attribute if the form is invalid.
173+
4. Disable the Login and Register buttons using the `[disabled]` attribute if the form is *invalid* and *dirty*.
163174

164175
5. Define a custom validator for the password field that refuses weak passwords. We will consider that a password is strong if it satisfies all of these requirements:
165176
- Contains at least one uppercase character (regex `/^.*[A-Z]+.*$/`)
166177
- Contains at least one lowercase character (regex `/^.*[a-z]+.*$/`)
167178
- Has at least one non-alphanumeric character (regex `/^.*\W+.*$/`)
168179
- Minimum length of 8 characters
169180

170-
Put that validator in `app/utils` and name it `password.validator.ts`. Here is its basic implementation:
181+
You can use the `test` method on each pattern and pass it the value of the control to check if the pattern exists in it.
182+
183+
Put that validator in `app/utils` and name it `password.validator.ts`.
184+
185+
::: details Help
186+
Here is its basic implementation:
171187

172188
```ts
173189
export function password(): ValidatorFn {
@@ -193,8 +209,9 @@ export function password(): ValidatorFn {
193209
}
194210
}
195211
```
212+
:::
196213

197-
You can use the `test` method on each pattern and pass it the value of the control to check if the pattern exists in it. Add an error text via the `<small>` tag on the password field that shows if the form is dirty and the field has the `password.pattern` error.
214+
Add an error text (`The password doesn't comply with the policy`) under the password field (use a `<small>` tag) that shows if the form is dirty and the field has the `password.pattern` error.
198215

199216
## Sources
200-
- [Angular official forms documentation](https://angular.io/guide/forms-overview)
217+
- [Angular official forms documentation](https://angular.dev/guide/forms-overview)

docs/src/fr/forms/README.md

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,37 +137,54 @@ Le composant suivant montre comment tirer parti :
137137
Vous pouvez trouver une liste à jour des classes [ici](https://angular.io/guide/form-validation#control-status-css-classes).
138138

139139
## TP : Connexion et inscription avec des reactive forms
140-
1. Implémentez le formulaire de connexion / inscription à l'aide de reactive forms et du form builder : remplacez le `[(ngModel)]` dans le template et supprimez les propriétés `email` et `mot de passe` de la classe du `LoginFormComponent`.
140+
1. Implémentez le formulaire de connexion / inscription à l'aide de reactive forms et du form builder : remplacez le `[(ngModel)]` dans le template et supprimez les propriétés `email` et `mot de passe` de la classe du `LoginFormComponent` et déclarez un `FormGroup` et nommez le `loginForm`.
141141

142142
2. Ajoutez le validateur required (`Validators.required`) aux deux champs et affichez le texte `"Ce champ est requis"` sous chaque champ si le formulaire a un statut dirty et que ce champ a l'erreur `required` :
143+
143144
```html
144145
<small>This field is required</small>
145146
```
146147

147148
::: tip Hint
148-
- Il est possible de récupérer une référence vers un *form control* en appelant la méthode `get('form control name')` du *form group*.
149-
- Il est possible de vérifier la présence d'une erreur spécifique en appelant la méthode `hasError('error name')` d'un *form control*.
149+
- Il est possible de récupérer la référence à une instance d'un `FormControl` en utilisant la propriété `controls` du `FormGroup` le contenant. Par exemple pour un `FormGroup` nommé *myFormGroup* et un `FormControl` nommé *myControl*:
150+
```ts
151+
myFormGroup.controls.myControl.touched
152+
```
153+
- Il est possible de vérifier si un `FormControl` (ou plus généralement un `AbstractControl`) a une erreur spécifique en appelant la méthode `hasError('error name')` de la classe `FormControl`.Par exemple pour un `FormGroup` nommé *myFormGroup*, un `FormControl` nommé *myControl* et une erreur *required* :
154+
```ts
155+
myFormGroup.controls.myControl.hasError('required')
156+
```
150157
:::
151158

152-
3. Stylisez le label et le texte d'erreur de chaque champ avec la classe `.ng-invalid` lorsque le formulaire est invalide et que ce champ n'est pas valide (rappelez-vous l'attribut `[class]`). Faites attention à ne bien styler que ces deux éléments pour chaque champ (vous pouvez utiliser le sélecteur CSS `+` à cet effet).
159+
3. Stylisez le label et le texte d'erreur de chaque input s'étant fait attribuer la classe `.ng-invalid` par angular mais uniquement lorsque le formulaire a la classe `ng-dirty`. Faites attention à ne bien styliser que ces deux éléments pour chaque input (vous pouvez utiliser le combinateur CSS `+` et la pseudo-classe `has()` à cet effet).
153160

154-
::: details Hint
155-
```scss
156-
label.ng-invalid, input.ng-invalid.ng-dirty + small {
161+
::: details Aide
162+
- [has() pseudo classe](https://developer.mozilla.org/en-US/docs/Web/CSS/:has)
163+
- [+ combinateur](https://developer.mozilla.org/en-US/docs/Web/CSS/Next-sibling_combinator)
164+
:::
165+
166+
::: details Correction
167+
```css
168+
form.ng-dirty label:has(+ input.ng-invalid), form.ng-dirty input.ng-invalid + small {
157169
color: red;
158170
}
159171
```
160172
:::
161173

162-
4. Désactivez les boutons Connexion et Inscription à l'aide de l'attribut `[disabled]` si le formulaire n'est pas valide.
174+
4. Désactivez les boutons Connexion et Inscription à l'aide de l'attribut `[disabled]` si le formulaire est *invalid* et *dirty*.
163175

164176
5. Définissez un validateur personnalisé pour le champ de mot de passe qui refuse les mots de passe faibles. Nous considérerons qu'un mot de passe est fort s'il satisfait à toutes ces exigences :
165177
- Contient au moins un caractère majuscule (regex `/^.*[A-Z]+.*$/`)
166178
- Contient au moins un caractère minuscule (regex `/^.*[a-z]+.*$/`)
167179
- Possède au moins un caractère non alphanumérique (regex `/^.*\W+.*$/`)
168180
- Longueur minimale de 8 caractères
169181

170-
Mettez ce validateur dans `app/utils` et nommez-le `password.validator.ts`. Voici son implémentation de base :
182+
Vous pouvez utiliser la méthode `test` sur chaque pattern/regex et leur passer la valeur du form control pour vérifier si le pattern y est présent.
183+
184+
Mettez ce validateur dans `app/utils` et nommez-le `password.validator.ts`.
185+
186+
::: details Aide
187+
Voici son implémentation de base :
171188

172189
```ts
173190
export function password(): ValidatorFn {
@@ -193,8 +210,9 @@ export function password(): ValidatorFn {
193210
}
194211
}
195212
```
213+
:::
196214

197-
Vous pouvez utiliser la méthode `test` sur chaque pattern/regex et leur passer la valeur du form control pour vérifier si le pattern y est présent. Ajoutez un texte d'erreur via la balise `<small>` dans le champ du mot de passe qui indique si le formulaire est invalide et si le champ contient l'erreur `password.pattern`.
215+
Ajoutez un texte d'erreur (`Mot de passe invalide`) via la balise `<small>` en dessous de l'input du mot de passe qui s'affiche si le formulaire est invalide et que l'input a l'erreur `password.pattern`.
198216

199217
## Sources
200-
- [Documentation officielle des formulaires Angular](https://angular.io/guide/forms-overview)
218+
- [Documentation officielle des formulaires Angular](https://angular.dev/guide/forms-overview)

0 commit comments

Comments
 (0)