Angular 6 Reactive Forms : How to set focus on first invalid input

前端 未结 6 1320
闹比i
闹比i 2020-12-16 18:21

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

相关标签:
6条回答
  • 2020-12-16 18:38

    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();
        }
      }
    }
    

    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.

    0 讨论(0)
  • 2020-12-16 18:42

    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;
    };
    

    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;
        }
      }
    }
    

    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.

    Here's a Working Sample StackBlitz for your ref.

    0 讨论(0)
  • 2020-12-16 18:43

    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;
         }
    }
    

    this.addressForm will be your FormGroup.

    We don't even need directive here.

    0 讨论(0)
  • 2020-12-16 18:46

    I did that using directives. So My form would look like this:

    <form [formGroup]="userForm" (submit)="saveData()" appFocus >
    ...
    </form>
    

    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();
        }
      }
    }
    

    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.

    0 讨论(0)
  • 2020-12-16 18:51

    This option does not work for me, but I managed to fix it by changing the code as follows:

    @Directive({
      selector: '[appErrorFocusin]'
    })
    export class ErrorFocusinDirective {
    
      selectorToFocus : String = 'textArea,mat-select,select,input,button';
    
      constructor(private el: ElementRef,
        @Inject(DOCUMENT) private document: Document) { }
    
      @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,:not(.mat-badge-hidden).mat-badge');
          let elementToOffset = this.getElementToOffset(formGroupInvalid[0]);
          this.document.documentElement.scrollTop = elementToOffset.offsetTop;
          this.setFocusOnError(elementToOffset);
        }
      }
    
    
      getElementToOffset(element : any){
        let defaultElement = element;
        while (!(element.parentElement instanceof HTMLFormElement)){
          if (element.parentElement){
            element = element.parentElement;
          }else{
            return defaultElement;
          }
        }
        return element;
      }
    
      setFocusOnError(elementToOffset : any){
        let listaElementos =     elementToOffset.querySelectorAll(this.selectorToFocus);
        if (listaElementos.length>0){
          listaElementos[0].focus();
        }
      }
    
    }
    
    0 讨论(0)
  • 2020-12-16 18:58

    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();  
    
        }
    
    0 讨论(0)
提交回复
热议问题