Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

306
Views
Resetting an Angular Reactive Form without triggering error rendering?

In this demo the login form username field renders in red after login is clicked, even though the form is reset like this:

  submit() {
    if (this.form.valid) {
      this.onLogin.emit(this.form.value);
      this.form.reset();
      this.form.markAsPristine();
      this.form.markAsUntouched();
      this.form.setErrors(null);
    }
  }

How do we keep the username field from painting red?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You have to add the required validator to your form in order to check the validation otherwise it will always return true for this.form.valid. It should be as follows :

 form: FormGroup = new FormGroup({
    username: new FormControl('',[Validators.required]),
    password: new FormControl('',[Validators.required]),
  });

Note : be sure you imported Validators from @angular/forms

import { Validators } from '@angular/forms';

and the error message should not be send from parent element because your are checking for errors when submitting the form so the submit() method should be as follows :

submit() {
if (this.form.valid && this.form.touched) {
    this.error = ''
    this.submitEM.emit(this.form.value);
}else{
    this.error = 'Username or password invalid'
}

}

Note : remove Input decorator for error message because the validation check is done in child component.

error: string | null;

Click here for demo

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!