1

I want to check whether the dropdown is empty. If not empty, enable the submit button. If empty, disable the submit button. Below is my html

<form  [formGroup]="addTaskForm"  (ngSubmit)="submit()" >
<mat-form-field>
  <mat-select placeholder="Favorite animal" [formControl]="animalControl" required>
    <mat-option>--</mat-option>
    <mat-option *ngFor="let animal of animals" [value]="animal">
      {{animal.name}}
    </mat-option>
  </mat-select>
  <mat-error *ngIf="animalControl.hasError('required')">Please choose an animal</mat-error>

</mat-form-field>
<mat-form-field>
  <mat-select placeholder="Favorite food" [formControl]="foodControl" required>
    <mat-option *ngFor="let food of foods" [value]="food.value">
      {{ food.viewValue }}
    </mat-option>
  </mat-select>
  <mat-error *ngIf="foodControl.hasError('required')">Please choose an food</mat-error>

</mat-form-field>

<div mat-dialog-actions>
    <button mat-button (click)="onNoClick()">Cancel</button>

<button type="submit"  mat-button cdkFocusInitial [disabled]="!formCtrl.form.valid">submit</button>
</div>
</form>

demo

help me out

5
  • 1
    There are multiple console errors in your demo that will probably lead you to a solution, if you correct them. Commented Mar 27, 2018 at 17:56
  • i am not getting any error in console Commented Mar 28, 2018 at 7:39
  • you definitely should be, if your code matches the demo you posted. if you aren't, then you probably should consider fixing the demo before asking for further help. Commented Mar 28, 2018 at 7:41
  • can you please help me out @Claies Commented Mar 28, 2018 at 7:58
  • I have updated my answer it is working please, try it Commented Mar 28, 2018 at 8:00

1 Answer 1

2

Please, Try below code it is working,

<form [formGroup]="addTaskForm"  (ngSubmit)="submit()" >
<mat-form-field>
  <mat-select placeholder="Favorite animal" [formControl]="animalControl" required>
    <mat-option>--</mat-option>
    <mat-option *ngFor="let animal of animals" [value]="animal">
      {{animal.name}}
    </mat-option>
  </mat-select>
  <mat-error *ngIf="animalControl.hasError('required')">Please choose an animal</mat-error>

</mat-form-field>
<mat-form-field>
  <mat-select placeholder="Favorite food" [formControl]="foodControl" required>
    <mat-option *ngFor="let food of foods" [value]="food.value">
      {{ food.viewValue }}
    </mat-option>
  </mat-select>
  <mat-error *ngIf="foodControl.hasError('required')">Please choose an food</mat-error>

</mat-form-field>

<div mat-dialog-actions>
    <button mat-button (click)="onNoClick()">Cancel</button>

<button type="submit"  mat-button cdkFocusInitial [disabled]="foodControl.hasError('required') || animalControl.hasError('required')">submit</button>
</div>
</form>
Sign up to request clarification or add additional context in comments.

5 Comments

where you bind selected value in dropdown
my expecttation before submit form all dropdown should be filled
no its not working only its checking foodControl i want to check both i updated here demo stackblitz.com/edit/angular-olzhfg-jhdvdn?file=app/…
i have changed my answer now try it
need one more help if i submit how can i get selected dropdown values in console(submit() { console.log(this.addTaskForm.value); }) . stackblitz.com/edit/angular-olzhfg-awghxy?file=app/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.