Skip to content

RC 6 Reactive Forms doesn't support disabled binding #11324

Closed
@naveedahmed1

Description

@naveedahmed1

I recently upgraded my project to RC 6. In my app we are using Reactive Forms. Among form fields there is a field which needs to be conditionally disabled.

Prior to RC 6 we were able to achieve this by applying disabled like like this.

<input formControlName="myField" type="text" [disabled]="form.controls.otherField.value">

After upgrade when I browse to that component, I see below warning in console.

It looks like you're using the disabled attribute with a reactive form directive. If you set disabled to true
      when you set up this control in your component class, the disabled attribute will actually be set in the DOM for you. We recommend using this approach to avoid 'changed after checked' errors.

      Example: 
      form = new FormGroup({
        first: new FormControl({value: 'Nancy', disabled: true}, Validators.required),
        last: new FormControl('Drew', Validators.required)
      });

I believe the the option to disable form field should be supported with Reactive Forms as well.

Currently to overcome this warning, we have to capture change event on otherField

(change)="onChange()"

onChange() {
        if (this.form.controls['otherField'].value) {
            this.form.controls['myField'].disabled = true;
        } else {
            this.form.controls['myField'].disabled = false;
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions