Formgroup.Valid starting as true IOINIC V3

Asked

Viewed 37 times

1

I am trying to validate a form that only receives a parameter, in this form I have a search button that should only be enabled after typing at least 5 letters, but is going wrong, pq the button initializes unlocked and when I start typing it locks and unlocks again when I type the 5 digits, I don’t use formGroup much, so I needed to know why it is starting as "True".

In short: the Valid form group should start as false, but is starting as true.

My TS:

this.formGroup = formBuilder.group({
  nmFornecedor: ['', Validators.minLength(5)]
})

HTML:

<form [formGroup]="formGroup">
  <ion-item>
    <ion-label floating>Nome fornecedor</ion-label>
    <ion-input type="text" formControlName="nmFornecedor"  ></ion-input>
  </ion-item> 

<button ion-button (click)="teste()" [disabled]="!formGroup.valid">Resultado</button>

</form>

1 answer

0

You can solve this problem in a very easy way by entering the attribute required in the input, you can see the example working here:

<form [formGroup]="formGroup">
  <ion-item>
    <ion-label floating>Nome fornecedor</ion-label>
    <ion-input type="text" formControlName="nmFornecedor" required></ion-input>
  </ion-item> 

  <button ion-button (click)="teste()" [disabled]="!formGroup.valid">Resultado</button>
</form>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.