Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/julien-marcou/nested-cva-validation
Reproduction repository for https://github.com/angular/angular/issues/42815
https://github.com/julien-marcou/nested-cva-validation
Last synced: 23 days ago
JSON representation
Reproduction repository for https://github.com/angular/angular/issues/42815
- Host: GitHub
- URL: https://github.com/julien-marcou/nested-cva-validation
- Owner: Julien-Marcou
- Created: 2022-12-24T14:33:58.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-18T16:14:03.000Z (12 months ago)
- Last Synced: 2023-11-18T17:48:43.855Z (12 months ago)
- Language: TypeScript
- Homepage:
- Size: 235 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Nested Validation
Reproduction repository for https://github.com/angular/angular/issues/42815
## Get started
- `npm install`
- `ng serve`
- open your browser on http://localhost:4200/## Description
When you'll open the application you'll see both FormGroups and nested CVA on the same page, to see the difference beetwen the two implementations.
Actually, the only difference you will see, is that the "validation" bug is only present for nested CVA.
The form with nested CVA is marked as "valid" even though is should not be, as there is some required fields that are not filled.You can submit the form to highlight the invalid fields for both implementations.
## CVA vs FormGroups
Here are some stats regarding the TypeScript files:
| | CVA | FormGroups |
|----------------------|----:|-----------:|
| File Count | 7 | 7 |
| Line Count (code) | 254 | 122 |
| Line Count (total) | 310 | 151 |
| Main Chunk Size (kB) | 152 | 150 |## Should I use FormGroups?
Well, both implementations are very different in terms of philosophy.
CVA allows you to directly control the structure of your FormControls from the Component itself, whereas the FormGroups approach tends to split the FormControls structure inside their own files, while still making them accessible from your components.IMO, making files smaller by splitting the component's logic (interactions between your HTML & TS file) and the form's data structure, makes it easier to maintain and test your code.
Also, this approach allows you to use the same FormGroup for 2 different component integrations, as the data structure/validation may be the same at two different points of your website but with two very different designs.
And finally, the logic to mark all input as checked is IMO simpler with the `touched$` observable from FromGroups, than with the `appMarkAllAsTouched` directive for the CVA.But the main deciding factors will probably be the following for you:
### When using nested CVA:
You cannot access the structure/FormControls of nested CVA, you only get the value output of the entire stack.
This is both a bad & good thing, it makes nested components act as black boxes, you don't know how they work and you should not, but at the same times, it makes it impossible to easily change the state of a nested FormControl (like disabling a nested input when a root input has changed its value).### When using nested FormGroups:
You cannot easily build an entire stack of nested controls by simply giving the values to your components, you have to build the stack of controls first in your FormGroups and then give them to your components. Fortunately, even though it differs a bit from the CVA philosophy, at the end you still end up with a smaller amount of code.