Skip to content

Setting values on a disabled form control triggers valueChanges of the form #229

@erikm-of

Description

@erikm-of

Use case:
In our situation we have a subform where we can select a template. If a template is chosen, other subforms are updated with values from the template and the controls are disabled.

We are using valueChanges on the root form to conditionally setting the template values on the disabled controls (amongst quite some other things). When changing the template value, we are setting values on already disabled controls, which is done in ngx-sub-form.component.ts in the writeValue function there is some workaround implemented because of some reported bugs in angular forms regarding setting values on a disabled form.

const fgDisabled: boolean = this.formGroup.disabled;

this.formGroup.setValue(transformedValue, {
  emitEvent: false,
});

if (fgDisabled) {
  this.formGroup.disable();
}

Although the workaround 'fixes' this issue, it also triggers a valueChanges on the form again because of setting the formGroup to disabled again. I would expect that {emitEvent: false} should be used when disabling the formGroup again (just like is done when setting the value).

const fgDisabled: boolean = this.formGroup.disabled;

this.formGroup.setValue(transformedValue, {
  emitEvent: false,
});

if (fgDisabled) {
  this.formGroup.disable({ emitEvent: false });
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions