angular / angular/components

Easier Cross-field-validation for mat-form-field/mat-error combinations

Ouverte
#18,065 10 commentaires 14 réactions 0 personnes assignées Voir sur GitHub
area: material/form-field feature needs: discussion P5
Langage dominant
TypeScript
Étoiles
25k
Forks
6.8k
Merge moyen
1 j 8 h
PR mergées (30 j)
91

Description

#### Feature Description

In reactive forms, cross-field validation requires the validation logic to be placed with the parent form instead of the individual control.
The existing feature works fine, however, changing the relevant `mat-form-field`(s) into an error-state to enable showing underlying `mat-error`(s) has to be done by calling the relevant `FormControl`'s `setErrors()` function.

However, with a directive, we need only to perform validation on the parent form and return `ValidationErrors`. The child controls can themselves detect whether they should go into error-state or not, and the developer need not worry further about it.

First, we run a normal validator function on the form that does nothing but return `ValidationErrors`. Then, we pass the key(s) of the error(s) for which we want cross-validation, to the custom directive.
Please note how the `mat-error`'s `*ngIf` depends upon the error-set of the `FormControl`, just like in normal validation, although it would still work if it depended on the error set of the parent `FormGroup` (which is what we do currently).
```

Range Start

Value must be less than End

```

#### Use Case
Developer convenience and ease of use. Brings more consistency between normal validation and cross-validation. Allows child controls to 'inherit' some user-specified errors of the parent on `valueChanges`.

#### StackBlitz
https://stackblitz.com/edit/angular-cross-validation-directive

Guide de contribution

Ouvrir le guide de contribution

Piste de recherche

Commencez par l’exemple StackBlitz lié et examinez comment le formulaire parent réactif, les FormControls enfants, mat-form-field et mat-error interagissent actuellement. Le travail est terminé lorsqu’une directive peut recevoir les clés d’erreur sélectionnées du formulaire parent, les exposer sur les contrôles enfants concernés et permettre à mat-error d’utiliser l’état d’erreur du contrôle enfant sans appels manuels à setErrors().

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
angular, typescript
Domaine
frontend
Type d'issue
Fonctionnalité
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.