Problem with nested ControlValueAccessor and Validation
See original GitHub issueWhich @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.
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:
- Created 2 years ago
- Reactions:4
- Comments:7 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
On both stackblitz above, it seems that calling
updateValueAndValidity
in the CVA hierarchy’s deepest component’sngAfterViewInit
lifecycle could be a temporary alternative.