0

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;
      }
   }
}

9
  • show the ts file code Commented Apr 8, 2019 at 12:19
  • added the ts file code @PrashantPimpale Commented Apr 8, 2019 at 12:25
  • Expecting uploadForm code Commented Apr 8, 2019 at 12:26
  • I don't understand your question Commented Apr 8, 2019 at 12:30
  • where did you declared uploadForm? Commented Apr 8, 2019 at 12:31

5 Answers 5

0

try giving template variable for the input ie

<input .... #<tempvar name>="ngModel" />
Sign up to request clarification or add additional context in comments.

Comments

0

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> 

Comments

0

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

Comments

0

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

Comments

0

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.

Comments

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.