question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem with nested ControlValueAccessor and Validation

See original GitHub issue

Which @angular/* package(s) are the source of the bug?

forms

Is this a regression?

No

Description

In Reactive Forms, if you have two or more nested custom form components that implement ControlValueAccessor and Validator, the outer one doesn’t update its validation status until you make a change in the inner component.

It seems the validation runs initially only once from the outer component to the inner one, so if the inner component initially is invalid the outer doesn’t know anything until the next validation cycle.

In the example provided, initially “user-data” is invalid, so “complex-form” should be also invalid, but as you can see it updates its status once you start typing in the input fields.

status

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-ivy-nkr2be?file=src/app/complex-form.component.ts

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in

Angular 12.0.5

Anything else?

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
St1ccommented, Oct 19, 2022

is there any update on this? I don’t think reusable formGroup is a valid solution here. It should be possible to have multiple levels of CVAs without any fancy logic behind it. And currently it works for all scenarios - except one - initialisation of all formControls. So update from bottom to top works - when user updates controls, everything bubbles up to the parent form. But if the child is invalid at the form creation, this calls validate() methods from top to bottom, and it doesn’t update the parent components properly.

1reaction
lorpmtpcommented, Oct 10, 2022

On both stackblitz above, it seems that calling updateValueAndValidity in the CVA hierarchy’s deepest component’s ngAfterViewInit lifecycle could be a temporary alternative.

Read more comments on GitHub >

github_iconTop Results From Across the Web

angular validation state of a nested ControlValueAccessor is ...
Issue: at component initialization, the validation state of a sub-form is not correctly propagated in parents of more than one level. More ...
Read more >
Angular: Nested Reactive Forms Using ... - Medium
In this post, we will see how to implement nested reactive forms using composite control value accessors(CVAs). Kara Erickson of the Angular Core...
Read more >
Nested Reactive Forms Using ControlValueAccessors(CVAs)
In this post, we will see how to implement nested reactive forms using composite ... on Reactive Forms mainly FormGroups, FormControls, Validations etc....
Read more >
How to Use ControlValueAccessor to implement a Nested Form
This video is part of the Angular Forms In Depth Course - https://angular-university.io/course/angular-forms-courseCheck out the PDF E-Books ...
Read more >
Working with Angular forms in an enterprise environment
A bit of background · Input form fields · Form directives · Super-charged Control Value Accessor's · Form validation · Creating wizards with ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found