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
Copy file name to clipboardExpand all lines: docs/src/forms/README.md
+27-10Lines changed: 27 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -136,7 +136,7 @@ The following component shows an example of how to take advantage of:
136
136
You can find an updated list of classes [here](https://angular.io/guide/form-validation#control-status-css-classes).
137
137
138
138
## 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`.
140
140
141
141
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`:
142
142
@@ -145,29 +145,45 @@ You can find an updated list of classes [here](https://angular.io/guide/form-val
145
145
```
146
146
147
147
::: 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*:
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).
153
159
154
160
::: details Hint
155
-
```scss
156
-
label.ng-invalid, input.ng-invalid.ng-dirty + small {
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*.
163
174
164
175
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:
165
176
- Contains at least one uppercase character (regex `/^.*[A-Z]+.*$/`)
166
177
- Contains at least one lowercase character (regex `/^.*[a-z]+.*$/`)
167
178
- Has at least one non-alphanumeric character (regex `/^.*\W+.*$/`)
168
179
- Minimum length of 8 characters
169
180
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:
171
187
172
188
```ts
173
189
exportfunction password():ValidatorFn {
@@ -193,8 +209,9 @@ export function password(): ValidatorFn {
193
209
}
194
210
}
195
211
```
212
+
:::
196
213
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.
198
215
199
216
## Sources
200
-
-[Angular official forms documentation](https://angular.io/guide/forms-overview)
217
+
-[Angular official forms documentation](https://angular.dev/guide/forms-overview)
Copy file name to clipboardExpand all lines: docs/src/fr/forms/README.md
+29-11Lines changed: 29 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -137,37 +137,54 @@ Le composant suivant montre comment tirer parti :
137
137
Vous pouvez trouver une liste à jour des classes [ici](https://angular.io/guide/form-validation#control-status-css-classes).
138
138
139
139
## 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`.
141
141
142
142
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
+
143
144
```html
144
145
<small>This field is required</small>
145
146
```
146
147
147
148
::: 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* :
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).
153
160
154
-
::: details Hint
155
-
```scss
156
-
label.ng-invalid, input.ng-invalid.ng-dirty + small {
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*.
163
175
164
176
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 :
165
177
- Contient au moins un caractère majuscule (regex `/^.*[A-Z]+.*$/`)
166
178
- Contient au moins un caractère minuscule (regex `/^.*[a-z]+.*$/`)
167
179
- Possède au moins un caractère non alphanumérique (regex `/^.*\W+.*$/`)
168
180
- Longueur minimale de 8 caractères
169
181
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 :
171
188
172
189
```ts
173
190
exportfunction password():ValidatorFn {
@@ -193,8 +210,9 @@ export function password(): ValidatorFn {
193
210
}
194
211
}
195
212
```
213
+
:::
196
214
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`.
198
216
199
217
## 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