Skip to content

Disabled FormControls are enabled on form creation #304

@JoschuaSchneider

Description

@JoschuaSchneider

While working on the issue I mentioned in #303 we found another Problem when creating disabled controls.

When passing a disabled control like this:

public form = createForm<MyForm>({
  ...,
  formControls: {
    name: new FormControl({ value: '', disabled: true }),
  }
});

The FormGroup that the name control is added to is being set to "enabled" because the disabled$ observable is defaulted to of(false) which causes the formGroup.enable call here:
https://github.com/cloudnc/ngx-sub-form/blob/43dede0015975db59333abbc185964a15acb4262/projects/ngx-sub-form/src/lib/create-form.ts#L238C13-L238C13

This resets all disabled states that I initially pass to the FormControls.

I found two workarounds:

  1. Provide disabled$ as a never emitting observable:
public form = createForm<MyForm>({
  ...,
  disabled$: new Observable<boolean>(),
  formControls: {
    name: new FormControl({ value: '', disabled: true }),
  }
});
  1. Set the disabled state again in the constructor:
constructor() {
  this.form.formGroup.controls.name.disable();
}

This behaviour was unexpected as the new API also handles the default values via the FormControl constructors.

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