Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disabled FormControls are enabled on form creation #304

Open
JoschuaSchneider opened this issue Oct 16, 2023 · 0 comments
Open

Disabled FormControls are enabled on form creation #304

JoschuaSchneider opened this issue Oct 16, 2023 · 0 comments

Comments

@JoschuaSchneider
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant