嗨,我想知道如何在自定义验证指令中调用我的验证方法。
这是我的Range_length验证功能:
rangeLength: (option: ValidArgs): boolean => {
let param: number[] = <number[]>option.param;
return option.value.length >= param[0] && option.value.length <= param[1];
},
这是我的自定义验证指令:
import { Directive, Input } from '@angular/core';
import { AbstractControl, NG_VALIDATORS, Validator } from '@angular/forms';
import { FormValidator } from '@syncfusion/ej2-ng-inputs';
export function rangelenValidator(control: AbstractControl): { [key: string]: boolean } | null {
debugger
let range_length1: number = 5;
let range_length2: number = 8;
let result: boolean = (FormValidator as any).checkValidator.rangeLength({ value: control.value, param: range_length1, param2: range_length2 });
if (result === true) {
return null;
}
else {
return { 'rangelen': true };
}
}
@Directive({
selector: '[apprangelen]',
providers: [{ provide: NG_VALIDATORS, useExisting: rangelenValidatorDirective, multi: true }]
})
export class rangelenValidatorDirective implements Validator {
@Input('apprangelen') rangelen: any;
validate(control: AbstractControl): { [key: string]: any } | null {
return this.rangelen ? rangelenValidator(control) : null;
}
}
这里有两个参数要检查,但是我不知道如何传递这两个参数,并且不方便调用该方法并进行验证?
类似地,我具有如下所示的最大验证功能:
max: (option: ValidArgs): boolean => {
if (!isNaN(Number(option.value))) {
// Maximum rule validation for number
return +option.value <= option.param;
}
}
我使用了如下所示的方法,它有效
import { Directive, Input} from '@angular/core';
import { AbstractControl, NG_VALIDATORS, Validator} from '@angular/forms';
import {FormValidator} from '@syncfusion/ej2-ng-inputs';
export function maxValidator(control: AbstractControl): { [key: string]: boolean } | null {
debugger
let max_value : number = 24;
let result: boolean = (FormValidator as any).checkValidator.max({ value: control.value, param: max_value});
if (result === true) {
return null;
}
else {
return { 'maxo': true };
}
}
@Directive({
selector: '[appmax]',
providers: [{ provide: NG_VALIDATORS, useExisting: maxValidatorDirective, multi: true }]
})
export class maxValidatorDirective implements Validator {
@Input('appmax') maxo: any;
validate(control: AbstractControl): { [key: string]: any } | null {
return this.maxo ? maxValidator(control) : null;
}
}