Skip to content
Snippets Groups Projects
error-transform.pipe.ts 1011 B
Newer Older
  • Learn to ignore specific revisions
  • import { Pipe, PipeTransform } from '@angular/core';
    import { ValidationErrors } from '@angular/forms';
    
    import { UIElement } from 'common/models/elements/element';
    
    
    @Pipe({
      name: 'errorTransform'
    })
    export class ErrorTransformPipe implements PipeTransform {
    
    rhenck's avatar
    rhenck committed
      transform(validationErrors: ValidationErrors, elementModel: UIElement): string {
    
        const validationMessages = ErrorTransformPipe.getValidationMessages(elementModel);
    
        let returnMessage = '';
    
        Object.keys(validationErrors).forEach(errorKey => {
          if (returnMessage) {
            returnMessage += '; ';
          }
    
    jojohoch's avatar
    jojohoch committed
          returnMessage += validationMessages[errorKey];
    
        });
        return returnMessage;
      }
    
    
      private static getValidationMessages = (elementModel: UIElement): Record<string, string> => ({
    
    jojohoch's avatar
    jojohoch committed
        required: elementModel.requiredWarnMessage as string,
    
        minlength: elementModel.minLengthWarnMessage as string,
        maxlength: elementModel.maxLengthWarnMessage as string,
    
    jojohoch's avatar
    jojohoch committed
        pattern: elementModel.patternWarnMessage as string
      });