Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

308
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda