question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RC6 Forms: Calling disabled() on FormControl makes entire form invalid

See original GitHub issue

I’m submitting a … (check one with “x”)

[X] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior If I call this.myForm.controls["code"].disable();, this.myForm.valid is always false:

this.myForm.patchValue({code: 17});
console.log(this.myForm.valid);
this.myForm.controls["code"].disable();
console.log(this.myForm.valid);

This outputs

true
false

Expected/desired behavior The form should still be valid, even though the control has been disabled.

Reproduction of the problem http://plnkr.co/edit/B8cA0zzcrt4SjxrGMlRf?p=preview

  • Angular version: 2.0.0-rc.6
  • Browser: [Chrome 53.0.2785.92 | Firefox 48 ]
  • Language: [TypeScript 1.8]
  • Remarks

I initially remarked this as a comment in #11271, but I don’t want to hijack the other issue, which is about dynamically binding [disabled]. It could be that PR #11257 fixes this already, but I am not sure, so I decided to file this bug-report.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:7
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

13reactions
unsafecodecommented, Sep 13, 2016

I’ve just stubbed this custom directive, which should behave as desidered. Warning: it’s just a proof-of-concept, not a complete solution. Feedbacks are welcome, of course.

@Directive({
  selector: '[formControlName][dynamicDisable]'
})
export class DynamicDisable implements OnInit, OnChanges {
  constructor(
    @Optional() @Host() @SkipSelf() private parent: ControlContainer,
  ) { 

  }

  @Input() formControlName: string;  
  @Input() dynamicDisable: boolean;

  private ctrl: AbstractControl;

  ngOnInit() { 
    if(this.parent && this.parent["form"]) {
      this.ctrl = (<FormGroup>this.parent["form"]).get(this.formControlName);
    }
  }

  ngOnChanges() {
    if (!this.ctrl) return;

    if (this.dynamicDisable) {
      this.ctrl.disable();
    }
    else {
      this.ctrl.enable();
    }
  }
}
<form [formGroup]="form">
    <label>
        Make required
        <input type="checkbox" formControlName="isRequired" [(ngModel)]="isRequired" />
    </label>
    <p>
        <input type="text" formControlName="yourName" [(ngModel)]="yourName" [dynamicDisable]="!isRequired" />
    </p>
    <p>
        Valid: {{form.valid}}
    </p>
</form>
@Component({
  selector: 'home',
  styleUrls: ['./home.css'],
  templateUrl: './home.html'
})
export class Home {

  form: FormGroup;
  isRequired: boolean = true;
  yourName: string = '';

  ngOnInit() {
    this.form = new FormGroup({
      "isRequired": new FormControl(true),
      "yourName": new FormControl('', [])
    });
2reactions
karacommented, Sep 7, 2016

[copied from #11257]

@capi The way the model works is that a parent form group is disabled if all its child controls are disabled. The status of group controls is always calculated as a reduction of the child controls.

In your example, your form only has one form control. Once you disable it, you are effectively disabling the whole form because all its (one) children are disabled. If you log the status of the form, you’ll see that its status is ‘DISABLED’, which is why valid is false. The statuses are mutually exclusive. Closing, as this works as designed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

angular - Reactive forms - disabled attribute - Stack Overflow
I have been using [attr.disabled] because I still like this template driven than programmatic enable()/disable() as it is superior IMO.
Read more >
Disabling Form Controls When Working With Reactive Forms ...
Instantiate a new FormControl with the disabled property set to true. FormControl({value: '', disabled: true}) . Calling control.disable() or control.enable() .
Read more >
Angular Custom Form Controls - Complete Guide
In this guide, we are going to learn exactly how to take an existing custom form control component and make it fully compatible...
Read more >
FormGroup - Angular
Tracks the value and validity state of a group of FormControl instances. ... For example, if one of the controls in a group...
Read more >
Angular 6: Disabling Input On Reactive Form Without ...
Each batch can contain several Using Angular Reactive Forms FormArray we can ... RC6 Forms: Calling disabled on FormControl makes entire form RC6...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found