Home > Software engineering >  Angular FormGroup touched not working for checkbox
Angular FormGroup touched not working for checkbox

Time:12-17

Angular form for checking form is touched not working with the checkbox. I got !newDeviceGroup.touched = true even though the checkbox is changing its value when I am clicking. Not sure why?

<form [formGroup]="newDeviceGroup" (ngSubmit)="onSubmit()">
  <div >
    <div >
      <label  for="isNewDevice"
        ><span >Device is work</span>

        <input
          type="checkbox"
          id="isNewDevice"
          name="isNewDevice"
          
          [value]="isDeviceWork"
          [checked]="isDeviceWork == true"
          (change)="onCheckChange($event)"
        />
        <span ></span>
      </label>
    </div>
  </div>
<button type="submit"  id="btnSubmit" [disabled]="!newDeviceGroup.touched">
</form>

CodePudding user response:

You need set the FormGroup as touched via AbstractControl.markAsTouched().

onCheckChange(event: any) {
  this.newDeviceGroup.markAsTouched();
}

Sample Demo on StackBlitz

  • Related