Newer
Older
import { Pipe, PipeTransform } from '@angular/core';
import { ValidationErrors } from '@angular/forms';
import { TranslateService } from '@ngx-translate/core';
@Pipe({
name: 'errorTransform'
})
export class ErrorTransformPipe implements PipeTransform {
constructor(private translateService: TranslateService) {}
transform(validationErrors: ValidationErrors, elementModel: UIElement): string {
const validationMessages = this.getValidationMessages(elementModel);
let returnMessage = '';
Object.keys(validationErrors).forEach(errorKey => {
if (returnMessage) {
returnMessage += '; ';
}
const messageKey = errorKey === 'required' && elementModel.type === 'checkbox' ? 'requiredTrue' : errorKey;
returnMessage += validationMessages[messageKey];
});
return returnMessage;
}
private getValidationMessages(elementModel: UIElement): Record<string, string> {
this.translateService.instant('validators.inputRequired'),
this.translateService.instant('validators.inputRequiredTrue'),
this.translateService.instant('validators.inputTooShort'),
this.translateService.instant('validators.inputTooLong'),