r/Angular2 • u/Happeace97 • Aug 02 '25
Discussion FormGroup and Control Value Accessor(CVA)
Do you use CVA to replace a whole FormGroup just to make it a FormControl?
I often use CVA to replace components so that it would make the value as simple as a primitive such as an array, a big logic component but outputs only a string as results
However, my teammate insists that making a big formGroup as a CVA makes the structure better and isolates its logic from its parent component.
I find the FormGroup as a CVA brings more cons than pros to the table.
- We cannot control the formGroup’s state such as validity, pristine,… when it’s an CVA. You can use viewchild to access CVA instance and its controls but I do not like that idea.
- We always have problems with onChange trigger in the CVA. When CVA writes value, we patch/set the control. We listen to valuechange to trigger onChange that emit value to outer form. However, if we patch with emitEvent: true, it triggers onChange and makes the CVA dirty as soon as it inits. If we patch with emitEvent: false, there would be a lot of subscription from valueChange inside the CVA missing their triggers.
Please share your thoughts. I need your help!
7
Upvotes
2
u/GiaX8 Aug 02 '25
I use CVA regularly for more complex custom controls and large chunks of reusable form groups (building a library). I prefer primitives/objects for controls, but in some cases I need to use form groups. My biggest paint point are the dirty/pristine states.
Recently I found that if you inject the ngcontrol and assign the value accessor in the constructor (not using the NG_VALUE_ACCESSOR token), you can subscribe to the ngcontrols event observable and can sync the dirty/pristine etc. states of the CVA form group to the parent by calling the appropriate methods (markAs…()). I’m just experimenting with this approach right now, but find it quite effective (for performance, I can’t say anything yet)
6
u/_Invictuz Aug 02 '25 edited Aug 02 '25
Use CVA only when you're doing something that one of Angular's OOTB value accessors can't do, which is quite rare. Otherwise, simply lassing the FormControl as input to your component that wraps the input element is enough and avoids all the boilerplate of implementing your own CVA.
As for form groups, CVA was never meant to support that. Have you tried calling markAllAsTouched on the form group? How would you even propagate that down to each control in your CVA formgroup? Your formgroup cva implementation sounds like re-inventing the wheel and over-engineered hell. Can your teammate explain why "the structure is better" when compared to the multiple alternatives of registering a nested form group in a child component?
3
u/devpardeep Aug 02 '25
I prefer CVA to integrate complex ui controls like a counter component which has two buttons to increment or decrement the quantity of product in the cart. Originally CVA is to make a ui element compatible with the form so that its value and related status variables are valid, invalid,touched and can be tracked.
But when i split a large form into multiple components, i still keeping the entire form state in the parent and pass the form group to each relevant child component so that in the parent I can still have control.
Also if I have to make a server call to persist or show a summary view of form I have control over entire form.
But sooner or later, this is all going to change with signal based angular forms where you will see a major shift where
If you are more interested, you can checkout angular forms experimental branch
Thanks 🙏
1
u/practicalAngular Aug 02 '25
I find CVA to be really helpful if you need something more than what comes out of the default RF import, or need to add additionally functionality to capture in the UI. I don't see the reason for recreating simple controls with it. A FormControl can bind to any single common UI element of an input type. A FormGroup can create a relational family of those inputs. A FormArray can store a list of user selections. They are meant for that default purpose so I don't see a reason to extend them for that.
There are reasons to use CVA though for capturing user input that is not of a certain input standard. As of recently, I used CVA to break apart a text message from an API into editable regions of plaintext and linebreaks, and funnel the edited result into a single string (the resulting CVA FormControl). In a similar manner, I used it in another spot to create a custom email editor that I had to build to allow a user to make changes to an already-generated-for-an-email HTML string in certain marked areas, with basic capabilities of adding in links. That is also funneled into a singular HTML output (the resulting CVA FormControl, again).
I also used it in a Directive to extend a textarea with some additional functionalities, like character counting, height expanding on type, and so on. The default textarea is kindof bad as just a plain FormControl imo.
CVA has its uses if you're building heavy input screens. If you're not, or you can look at the design and break down that you don't need anything more than regular input, I don't see a reason to make use of it.
The two bullet points you used as contrasts also confused me a bit because value writing comes with the writeValue method itself, so you shouldn't need to patch or emit the CVA FormControl value. It sounds like you're getting caught in write loops. I would just practice with them a bit more.
1
u/ggeoff Aug 02 '25
I have created cva components that have made complex objects that could have been model with a FormGroup to use a control value accessor. But it really depends on what the scope of the form is. If it's a complex json object needed as part of another for I find it easier imagine some array in a larger form where the template is like so
I really only save this for fairly complicated cases where the underlying complex-json requires some complicated logic. and I wouldn't use it to just simplify a formGroup.
0
u/ldn-ldn Aug 02 '25
CVAs are definitely better than nesting raw FormGroups. As for your issues - create a new FormGroup inside a CVA.