0
votes

I have a reactive form where the submit button should be disabled until aa inputs are filled

I have tried using ngmodel and ngForm parameters to disable the button but it is not enabled when the inputs are filled

this is my code

<form #uploadForm="ngForm">
    <div class="uploadDiv">
  <div *ngFor="let data of fileList, let i = index">

    <label class="adpLabel">{{data.fileDesc}}</label>   
    <input readonly class="adpInput" type="text" [(ngModel)]='listFilter' name="listFilter" value={{filename[i]}}>
    <input type="file" id="{{data.fileName}}"
    #selectFiles hidden accept=".xls,.xlsx" (change)="getFileInfo($event, i)">
    <button mat-button (click)="selectFiles.click()" class="browseBtn">Browse</button>    
  </div>

<div class="adpButtons"> 
        <button mat-button [disabled]="!uploadForm.valid" (click)="clickFileUpload()">Upload</button>
        <button mat-button disableRipple tabindex="-1" mat-dialog-close>Back</button>
</div>

    </div>
</form>

I expect the upload button to be enabled when input is filled

EDIT 1: Adding ts code

getFileInfo(event, i) {
   if(this.file[i]){
      this.file.splice(i,1,event.target.files);
      this.fileType.splice(i,1,event.target.id);
   }
   else{
      this.file[i]=event.target.files;
      this.fileType[i]=event.target.id;
      //this.file.push(event.target.files);
      //this.fileType.push(event.target.id);
   }
   for (let j = 0; j < this.file.length; j++) {
      let fileName:string='';
      let extension:string;
      if(event.target.files[j]){
       fileName=event.target.files[j].name;
       extension = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
      } 
      let id:any; 
      if(this.fileType[j]){
        id=this.fileType[j];
      }
      if ( extension==".xls" || extension==".xlsx" ) {
        this.filename[i] = fileName; 
        this.fileType[i] = id;
      }
   }
}

5
show the ts file code - Prashant Pimpale
added the ts file code @PrashantPimpale - Danny
Expecting uploadForm code - Prashant Pimpale
I don't understand your question - Danny
where did you declared uploadForm? - Prashant Pimpale

5 Answers

0
votes

try giving template variable for the input ie

<input .... #<tempvar name>="ngModel" />
0
votes

You are not really using Angular Reactive Forms - or at least not correctly. https://angular.io/guide/reactive-forms

You define a Formgroup and Controls in your .ts file, a form with controls in your .html file and connect both.

https://stackblitz.com/edit/angular-reactive-forms?file=app%2Fapp.component.html

.ts

SignupForm: FormGroup;

this.SignupForm = new FormGroup({
 'username':new FormControl(null),
 'email':new FormControl(null)
});

.html

<form [formGroup]="SignupForm" (ngSubmit)="onSubmit()">
 <input type="text" formControlName = "username">
</form>

With this setup you can get controls from the formgroup and evaluate their status

.ts

  get controlUsername() {
   return this.SignupForm.get('username');
  }

.html

<button [disabled]="!controlUsername.valid" type="submit">Submit</button> 
0
votes

Instead of NgModel use FormControlName to validate. For reference: https://angular.io/guide/reactive-forms

0
votes

If you want to check if there are any files selected or not to allow a user to submit the request then check with the length of fileList

<button mat-button [disabled]="fileList.length == 0" (click)="clickFileUpload()">Upload</button>

Here is Stackblitz

0
votes

You can use the required attribute on the input field like

<input type="file" id="{{data.fileName}}"
#selectFiles hidden accept=".xls,.xlsx" (change)="getFileInfo($event, i)" required>

And also just a suggestion, try to use reactive forms as much as possible. It's so simpler and has a lot of options when compared to template-driven forms.