Easier Cross-field-validation for mat-form-field/mat-error combinations
- Lingua principale
- TypeScript
- Stelle
- 25k
- Fork
- 6.8k
- Merge medio
- 1g 8h
- PR unite (30g)
- 91
Descrizione
#### 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
Guida per i contributori
Apri la guida per i contributori
Direzione di ricerca
Inizia con l'esempio StackBlitz collegato e verifica come interagiscono attualmente il form padre reattivo, i FormControls figli, mat-form-field e mat-error. Il lavoro è completo quando una direttiva può ricevere le chiavi di errore selezionate del form padre, esporle sui controlli figli interessati e consentire a mat-error di usare lo stato di errore del controllo figlio senza chiamate manuali a setErrors().
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- angular, typescript
- Ambito
- frontend
- Tipo di issue
- Funzionalità
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100