我有兩個定製validator
在reactive form
,我調用下面函數來創建在部件的構造形式爲:多個定製檢驗角2
private createForm(): void {
this.passwordUpdateForm = this.formBuilder.group({
newpassword : [null, Validators.required],
passwordconfirm: [null, Validators.required]
},
{
validator: [PasswordValidation.PasswordMatch, PasswordValidation.PasswordRule] // validation method
});
}
PasswordValidation是具有類兩個功能如下
export class PasswordValidation {
public static PasswordMatch(control: AbstractControl) {
let password = control.get('newpassword'); // to get value in input tag
if(password){
let confirmPassword = control.get('passwordconfirm').value; // to get value in input tag
if (password.value !== confirmPassword) {
control.get('passwordconfirm').setErrors({ ['passwordmatch'] : true});
}else {
return null;
}
}
}
public static PasswordRule(control: AbstractControl) {
let password = control.get('newpassword').value; // to get value in input tag
let pattern = new RegExp('^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#[email protected]$%^&*-]).{8,64}');
if (!pattern.test(password)) {
control.get('newpassword').setErrors({ ['passwordrule'] : true});
}else if (password.toLowerCase() === 'something') {
control.get('newpassword').setErrors({ ['passwordrule'] : true});
}else {
return null;
}
}
}
每個自定義驗證工作正常分別是這樣
validator: PasswordValidation.PasswordMatch
或本
validator: PasswordValidation.PasswordRule
但在陣列狀
validator: [PasswordValidation.PasswordMatch, PasswordValidation.PasswordRule]
得到錯誤this.validator is not a function
使用他們與不工作,我沒有任何想法,請幫幫我。
你可以嘗試用'Validators.compose()',它接受多個驗證對一個領域。 – Niladri
是使用compose():驗證器:Validators.compose([[PasswordValidation.PasswordMatch,PasswordValidation.PasswordRule])} – Vega
@Vega不起作用 – Parid0kht