0

I have a complex set of objects that I need to edit. Basically, I have several objects at the top level, which may or may not be enabled, and each one has some complex sub-objects. The whole set of objects need to be saved together at once. To do this I created a form, and each object is considered a "section" that implements a custom form control. That way from the parent component can deal with the application state and I could set up a reactive form with everything typed out, etc.

Each of the child components will implement ControlValueAccessor and Validator interfaces. These components use an inner "form" (FormGroup) to track their state. When writeValue is called I use setForm and pass { emitEventand: false } to not trigger the valueChanges observable that will invoke the onChanged "event". After wiring up the NG_VALUE_ACCESSOR and NG_VALIDATORS I am getting the values to flow from the parent form to the child forms.

The issue I'm running into is that when I'm trying to enable/disable components based on other components values. In simpler forms I would wire up a valueChanges subscription and then call enable/disable on the appropriate FormControl. When I'm doing it in this nested fashion it doesn't seem to pick that value up until I've toggled the checkbox once. Matter of fact, once I add in the NG_VALUE_ACCESSOR provider it seems to do this.

What am I doing wrong?

Here's a stackblitz example showing the exact issue: https://stackblitz.com/edit/v96wamwy-crtxjtvz?file=src%2Fapp%2Finner-form%2Finner-form.component.ts

2
  • Sorry, could you explain in a simpler manner what are you trying to achieve with the stackblitz demo? what is the "happy path" that isn't working? Commented Nov 2 at 6:45
  • @Andres2142 The goal is that when the outer form has the complex property that has a sub form set to disabled that it persists to the inner form. I believe the I may have figured out the issue (and may have inadvertently updated the stackblitz example) with respect to how events are emitted during the writeValue call. Commented Nov 3 at 14:56

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.