Skip to content

Why updateValueAndValidity is not updating the value of form Controls in Angular 12 #46983

@Asif905

Description

@Asif905

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

forms

Is this a regression?

Yes

Description

I have recently migrated from Angular 9 to Angular 12 and observing one issue related to form control while using ReactiveFormsModule.

The layout is like the page has a dropdown selection and multiple input boxes. And the behavior is if I change the value from dropdown it populates the values of the input box based on the model.

On selecting dropdown value, we are subscribing to value Changes and setting input box values using patchValue() method. I am also using updateValueAndValidity() to update the respective values. Below is the code for reference

toggleControls(show: boolean, formMeta: object, data?: { [key: string]: string }) {
        Object.keys(formMeta).forEach((formName) => {
            if (!show) {
                this.form.removeControl(formName);
                return;
            }
            this.form.addControl(formName, formMeta[formName]);
            this.form.get(formName).patchValue(data[formName]);
        });
        this.form.updateValueAndValidity();
    }

Issue:
using patchValue and updateValueAndValidity() is not updating the value of the form controls, and new set value is not getting reflected in the UI as well.

Note: If I manually trigger changeDetection after updateValueAndValidity(), the behavior works as expected.

Question: This behavior was working fine in Angular 9 and observing this issue after migrating to 12. Is there any change recently related to form value update and any alternative solution without using changeDetection..?

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

No response

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: formsbugneeds reproductionThis issue needs a reproduction in order for the team to investigate furtherregressionIndicates than the issue relates to something that worked in a previous version

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions