How to display only single validation error at a time

后端 未结 6 1655
失恋的感觉
失恋的感觉 2021-02-08 02:35

I have this code to which displaying errors on my form


&         


        
6条回答
  •  走了就别回头了
    2021-02-08 03:04

    You can create a Custom Pipe that checks first error equals with specified error:

    CUSTOM PIPE

    import { Pipe, PipeTransform } from '@angular/core';
    
    @Pipe({
      name: 'equals'
    })
    
    export class Equals implements PipeTransform {
    
      transform(errors: any, error: any, args?: any): any {
        if (!errors)
          return false;
    
        const array = Object.keys(errors);
        if (array && array.length > 0)
          return errors[array[0]] === error;
    
        return false;
      }
    }
    

    You can have lots of error div but just one error will be shown:

    // input is form.controls.input1
    
    Required
    MaxLength
    Pattern

提交回复
热议问题