asotog
asotog

Reputation: 225

How to fire/trigger all fields form validation in Angular 2?

Is there any way to for example, click on submit button and then trigger all the validations of the form fields, noticed it shows error messages from beginning if not checking dirty or touched which is fine, but what if i don't have to show messages from the beginning ?

use case: for user to view all the fields that are missing or wrong before submitting data

Upvotes: 0

Views: 581

Answers (1)

Bazinga
Bazinga

Reputation: 11214

You can do this:

<form novalidate (submit)="submit(loginForm)">

   <input type="email" formControlName="email">

   <p *ngIf="loginForm.get('email').hasError('required') && submitted">This field is required</p>

   <button type="submit" class="btn btn-info">Submit</button>

</form>

And in your component:

submit( form : FormGroup ) {
   this.submitted = true;
   if( form.invalid ) return;
}

Upvotes: 1

Related Questions