angular / angular/components

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

Offen
#18,065 10 Kommentare 14 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
area: material/form-field feature needs: discussion P5
Vorherrschende Sprache
TypeScript
Sterne
25k
Forks
6.8k
Ø Merge
1 T. 8 Std.
Gemergte PRs (30 T.)
91

Beschreibung

#### 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

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne mit dem verknüpften StackBlitz-Beispiel und überprüfe, wie das reaktive übergeordnete Formular, die untergeordneten FormControls, mat-form-field und mat-error derzeit zusammenspielen. Die Aufgabe ist erfüllt, wenn eine Direktive ausgewählte Fehler-Schlüssel des übergeordneten Formulars empfangen, sie auf den entsprechenden untergeordneten Controls verfügbar machen und mat-error den Fehlerstatus des untergeordneten Controls ohne manuelle setErrors()-Aufrufe verwenden lassen kann.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
angular, typescript
Bereich
frontend
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.