Angular 6 Reactive Forms : How To Set Focus On First Invalid Input January 09, 2023 Post a Comment Under my Angular 6 app , i'm using Reactive Forms . My purpose is when submitting , i want to set focus on first invalid input when error. My form looks like this : Solution 1: Use below code in your submit. for (const key of Object.keys(this.addressForm.controls)) { if (this.addressForm.controls[key].invalid) { const invalidControl = this.el.nativeElement.querySelector('[formcontrolname="' + key + '"]'); invalidControl.focus(); break; } } Copy this.addressForm will be your FormGroup. We don't even need directive here. Solution 2: My Answer is inspired from yurzui's answer here. I'm using the logic from his answer to get the nativeElement of a particular FormControl by using it's FormControl. This is the logic that does that: const originFormControlNameNgOnChanges = FormControlName.prototype.ngOnChanges; FormControlName.prototype.ngOnChanges = function () { const result = originFormControlNameNgOnChanges.apply(this, arguments); this.control.nativeElement = this.valueAccessor._elementRef.nativeElement; return result; }; Copy Now, the form's errors field would be null even though it's fields are invalid. So to get the exact first field that's invalid, we'll have to loop through all the fields and check for validity for each of them. I can write this logic in the onSubmitForm() method. Something like this: onSubmitForm() { const fieldsToCheck = [ 'codeBasicat', 'libellePef', 'nomApplication' ]; for (let i = 0; i < fieldsToCheck.length; i++) { const fieldName = fieldsToCheck[i]; if (this.addItemfForm.get(fieldName).invalid) { ( < any > this.addItemfForm.get(fieldName)).nativeElement.focus(); break; } } } Copy I've deliberately used for instead of Array.forEach as I wanted to break from the loop. Hopefully this should do the trick for you.Baca JugaHow To Add Labels To Bootstrap Dialog FooterParsing Table For A LinkGetimagedata Always Returning 0 Here's a Working Sample StackBlitz for your ref. Solution 3: I did that using directives. So My form would look like this: <form [formGroup]="userForm" (submit)="saveData()" appFocus > ... </form> Copy and the code for the directive itself: import { Directive, HostListener, Input, ElementRef } from '@angular/core'; import { NgForm } from '@angular/forms'; @Directive({ selector: '[appFocus]' }) export class FocusDirective { constructor(private el: ElementRef) { } @Input() formGroup: NgForm; @HostListener('submit', ['$event']) public onSubmit(event): void { if ('INVALID' === this.formGroup.status) { event.preventDefault(); const formGroupInvalid = this.el.nativeElement.querySelectorAll('.ng-invalid'); (<HTMLInputElement>formGroupInvalid[0]).focus(); } } } Copy However this solution is incomplete as there is a lot of corner cases that have to be considered. For example what if the first element is radio button group. Dispatching focus event will automatically mark the filed. Second not every element to which angular ads ng-invalid will be an input. Solution 4: We can set focus on first invalid input simply by just write this code in the submit() of the form. if(this.form.invalid) { // Got focus to the error field let invalidFields = [].slice.call(document.getElementsByClassName('ng-invalid')); invalidFields[1].focus(); } Copy Solution 5: Try this: import { Directive, HostListener, ElementRef} from '@angular/core'; @Directive({ selector: '[focusFirstInvalidField]' }) export class FocusFirstInvalidFieldDirective { constructor(private el: ElementRef) { } @HostListener('submit') onFormSubmit() { const invalidElements = this.el.nativeElement.querySelectorAll('.ng-invalid'); if (invalidElements.length > 0) { console.log(invalidElements[0]); invalidElements[0].focus(); } } } Copy Remember to debug, see if element 0 is not your own form as it happened to me, so see right what field it is reporting as the first and put the position right. Share You may like these postsHow To Show Images In Img Tag Inside An Ng-repeat While Using Angular Js?Canvas Html5 Javascript Code Not Working, With Canvas.todataurl()Window Popups - How To Get Window.blur() Or Window.focus() To Work In Firefox 4?How Can I Create A "first-load" Event In Html Or Javascript? Post a Comment for "Angular 6 Reactive Forms : How To Set Focus On First Invalid Input"
Post a Comment for "Angular 6 Reactive Forms : How To Set Focus On First Invalid Input"