Your nested Angular form may be better off with implementing ControlValueAccessor
Tags: angular, reactiveforms, nested, controlvalueaccessor
Published: 2021-06-04 19:46:22

This post has but one message for Angular users: Your nested form implementation may be better off with a ControlValueAccessor.

I am not the first one [to do this](https://stackoverflow.com/a/55674946). Also see [this post](https://dev.to/julianobrasil/writing-nested-recursive-angular-reactive-forms-a-clean-approach-57fk) by @julianobrasil. Nevertheless a disclaimer and a request: I've had about four weeks of professional Angular development experience, [apply healthy skepticism](https://brandur.org/fragments/engineering-skepticism) with abundance. I'm writing to clarify my understanding, and give something to the community. If you think the claim is preposterous, reach out!

Now, let's get you convinced by building up a minimally realistic example.

Suppose we have a form that used to input how many customers we can accept in a given day. Initially, the form is very simple. There is one integer input field per day. The only validation requirement is that the capacity can't be smaller than 0 or larger than 20.

We implement this with one component that has a FormGroup property and bind the controls manually. This is the simplest use of Angular Reactive Forms. You can find a barebones example [here](https://stackblitz.com/github/oguzhanogreden/example-nested-form-cva/tree/0fb88df86ee9cedd231a7fde629d18ae9f51fcec).

Now let's imagine a more complex second iteration for the form. We need to be able to support varying capacity throughout the day. That is, instead of a single integer input, we need to allow the user to add/remove time periods, which are specified by a start time, an end time and a capacity. We need to make sure that user can't enter an end time that's earlier than the start time. And we need to make sure that the total capacity doesn't exceed 20 within a day.

Conceptually, we have two levels here: the time period level and the day level. We can implement two components, and nest the time period component in the day component. We can then reuse the time period component within and across days, and the day component across days.

Here's my solution, where each component implements ControlValueAccessor.

 {% stackblitz example-nested-form-controlvalueaccessor view=preview %} 

I think there are three key implementation details worth noting:

1. Components that implement `ControlValueAccessor` interface bind to `FormControl`s, and not to a `FormGroup` or `FormArray`.
2. You need to notify parents of value changes, just like the default value accessors for e.g. [number inputs](https://github.com/angular/angular/blob/11.2.13/packages/forms/src/directives/default_value_accessor.ts#L144-L148) do.

ControlValueAccessor interface is very helpful here, especially while handling nested arrays that do not have a predetermined size. The interface is called a **Control**ValueAccessor for a reason ;) Don't forget the point 1 above.

There is one final implementation detail:

3. You need to trigger change detection if a parent acts in a way that a child causes a change in the validity of the parent without triggering change detection automatically. This is the case e.g. when the parent triggers initialisation of a child which initializes in invalid state.

In my limited experience, this implementation is the most flexible and the most idiomatic one. Do you disagree? I'd be happy to learn from you!