angular / angular/components

Allow mat-error in mat-form-field subviews

Ouverte
#15,778 0 commentaires 11 réactions 0 personnes assignées Voir sur GitHub
area: material/form-field P3
Langage dominant
TypeScript
Étoiles
25k
Forks
6.8k
Merge moyen
1 j 8 h
PR mergées (30 j)
91

Description

#### What is the expected behavior?
**NOTE**: it's more of an improvement (but a really critical one) than a bug.

`mat-error` should work even if it's not used as direct child of `mat-form-field`. It should work if it's used inside a component which itself is used in `mat-form-field`.

This is particularly necessary for **handling error messages in a reusable component**.
A real-world application may have several forms with a lot of validators on their fields.
Repeating a bunch of `mat-error`s and corresponding error messages for each usecase is just tedious.

### Suggested solution
I think the reason this currently doesn't work is because `@ContentChildren` decorator is used to get a hold of `mat-error` instances. However `@ContentChildren` [doesn't work for nested components](https://github.com/angular/angular/issues/5241) and aparently [this is by design](https://github.com/angular/angular/issues/5241#issuecomment-156841935).
Instead of using `@ContentChildren` , `MatError` can inject `MatFormField` and register itself into it.

#### What is the current behavior?
`mat-error` only works if it's directly used inside `mat-form-field`. If it's used inside a nested component, it doesn't work as expected.

#### What are the steps to reproduce?
https://stackblitz.com/angular/kllvoljyoem

#### Which versions of Angular, Material, OS, TypeScript, browsers are affected?
@angular/cdk@7.3.0
@angular/material@7.3.0
Angular: 7.2.3

#### Is there anything else we should know?

Guide de contribution

Ouvrir le guide de contribution

Piste de recherche

Commencez par la reproduction StackBlitz liée et examinez comment mat-form-field détecte les instances de mat-error à travers un composant réutilisable imbriqué. Confirmez la limitation actuelle aux enfants directs, puis vérifiez que les erreurs de validation s'affichent lorsque mat-error est imbriqué et que le comportement existant pour les enfants directs reste intact.

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
45/100

Recevez les nouvelles issues par e-mail

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