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

141
Views
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 answers
Answer question

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 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!