File
Methods
validate
|
validate(escapedMaxLength?: number)
|
|
Parameters :
Name |
Type |
Optional |
escapedMaxLength |
number
|
Yes
|
|
defaultErrorMessage
|
Type : string
|
Default value : ''
|
|
type: plugin
title: Escaped Max Length
description: A description for Escaped Max Length
runtimeCompatible: true
import { PluginValidator, PluginValidatorType } from '@allianz/taly-core';
import { Injectable, inject, Signal } from '@angular/core';
import { AbstractControl, ValidationErrors, ValidatorFn } from '@angular/forms';
import { LocalizeFn } from '@angular/localize/init';
import {
DEFAULT_OPTIONS,
EscapedMaxLengthValidatorPluginOptions
} from './escaped-max-length-validator.module';
import { ESCAPED_MAX_LENGTH_PLUGIN_OPTIONS } from './tokens';
declare let $localize: LocalizeFn;
@Injectable({ providedIn: 'root' })
export class EscapedMaxLengthValidator implements PluginValidator<number> {
private options = inject<Signal<EscapedMaxLengthValidatorPluginOptions>>(
ESCAPED_MAX_LENGTH_PLUGIN_OPTIONS
);
// To be specified when configuring this validator for a form field
type: PluginValidatorType = 'PLUGIN_MYTEAM_ESCAPED_MAX_LENGTH';
defaultErrorMessage = '';
private _setDefaultErrorMessage(max: number) {
this.defaultErrorMessage = $localize`:@@validation.error.escapedMaxLengthValidator:This field should not contain more than ${max} characters`;
}
validate(escapedMaxLength?: number): ValidatorFn {
const maxLength =
escapedMaxLength ?? this.options()?.escapedMaxLength ?? DEFAULT_OPTIONS.escapedMaxLength;
this._setDefaultErrorMessage(maxLength);
return (control: AbstractControl): ValidationErrors | null => {
if (!control.value) {
return null;
}
const maxLength =
escapedMaxLength ?? this.options()?.escapedMaxLength ?? DEFAULT_OPTIONS.escapedMaxLength;
this._setDefaultErrorMessage(maxLength);
const actualValue = JSON.stringify(control.value).slice(0, -1).substr(1);
if (actualValue.length > maxLength) {
return {
[this.type]: true
};
}
return null;
};
}
}