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

146
Visualizações
Angular: Scroll and focus to first invalid control in nested form once form has been submitted

I am trying to scroll and focus invalid control in form onsubmit. If I am using single form element its working as expected but when I am using nested form element not working.

please find the sample example here stackblitz.

How to scroll to invalid control and focus in nested forms on submit rootform.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think I've found a solution to your problem.

In your directive, inside the method scrollToFirstInvalidControl change the line:

const firstInvalidControl: any = this.el.nativeElement.querySelector('.ng-invalid');

with:

let firstInvalidControl: any;
    let all = document.getElementsByClassName('ng-invalid');
    for (var i = 0; i < all.length; i++) {
      if(all[i].tagName == 'INPUT'){
        firstInvalidControl = all[i];
        break;
    }
}

It will take all elements that have a ng-invalid class, but will check only those that have a tagName as INPUT - since your validation is for input fields, not whole forms as such (your original code caught first invalid form, but that wasn't the form that had an input that you wanted to scroll to).

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