Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show errors messages only after form submit

I am creating Angular 6 Form with validation. I want to show error messages only after submitting the form. Important fact is that messages SHOULD NOT change during the typing. So for example when the user hasn't typed anything in input and then submitted the form, the required messages should appear. After that when user types something, the messages should be visible all the time until the next submitt button press. Also the error messages shouldn't change to another when previous rule was fulfiled. To be honest I don't know if it is possible using Reactive Forms.

app.component.html

<form [formGroup]="form" novalidate (ngSubmit)="submit()" #myform="ngForm">
    <input class="input" type="text" formControlName="firstName" />   
    <p *ngIf="form.get('firstName').hasError('required') && myform.submitted">
      Name is required
    </p>
    <p *ngIf="form.get('firstName').hasError('minlength') && myform.submitted">
      Min length of string is {{form.controls.firstName.errors.minlength.requiredLength}}    
    </p>
    <button type="submit">Submit</button>
</form> 

app.component.ts

export class AppComponent  {
  form: FormGroup
  constructor(private fb: FormBuilder,) {
      this.form = this.fb.group({
          firstName: ['', [Validators.required, Validators.minLength(5)]]
      });
  }

  submit() {
    console.log(this.form);
  }
}

DEMO: stackblitz

Thanks for any help!

like image 224
hetious Avatar asked Feb 22 '19 08:02

hetious


People also ask

How do I display error messages in reactive form?

When multiple validations are involved in a single formControl, we need to put multiple *ngIf in the HTML and bind the respective validator's error messages which makes the code clumsy.

How do I display form submission errors and messages?

messages.success (request, 'Contact request submitted successfully.') If the form submission is invalid, show “Invalid form submission.” messages.error (request, 'Invalid form submission.') You can also display the actual error so that the user can understand what has gone wrong.

How do I add error messages to my form fields?

Simply add checking attributes to the HTML form fields, and the browser will automatically show the errors. For example, <input type="text" required/> Use Javascript to show custom error messages as the user types in the fields.

How do I show errors in HTML forms?

There are no fixed ways to show errors in HTML forms, but the common methods to display error messages are: Simply add checking attributes to the HTML form fields, and the browser will automatically show the errors. For example, <input type="text" required/>

How to make error message only appear after clicking submit button?

And yes, if you wanted the message to only appear after clicking Submit with invalid fields, I would try moving the conditional logic from Disabled to OnSelect, like you suggest, and never disabling the button. For instance, if you're unsatisfied with the error message being there from the start, I would try putting this in OnSelect:


2 Answers

From Angular 7 you can use: {updateOn:'submit'} or {updateOn:'blur'}

*Update using new FormGroup and new FormControl (using formBuilder not work)

Use {updateOn:'blur'} when you want to correct the error is lost focus the input, {updateOn:'submit'} if it's only when submit

this.form = new FormGroup({
        firstName:new FormControl('', 
          [Validators.required, Validators.minLength(5)])
      },{updateOn:'submit'});

*Update 2 if you want use with formBuilder see the answer to Q in stackoverflow

like image 116
Eliseo Avatar answered Oct 18 '22 04:10

Eliseo


You could check the validation in the submit function like this:

<p *ngIf="requiredError">
  Name is required
</p>
<p *ngIf="invalid">
  Min length of string is {{form.controls.firstName.errors.minlength.requiredLength}}    
</p>

  invalid: boolean;
  requiredError: boolean;
  submit() {
    console.log(this.form);
    this.invalid = this.form.get('firstName').hasError('minlength');
    this.requiredError = this.form.get('firstName').hasError('required');
  }
like image 44
ferhado Avatar answered Oct 18 '22 04:10

ferhado