Angular 如何在Ngonit方法中添加额外的验证器?
我有一个角度组件,我正在向构造函数中的表单添加一些验证器。但是,我想在我的ngOnit方法中添加额外的验证器。我怎样才能做到这一点Angular 如何在Ngonit方法中添加额外的验证器?,angular,typescript,Angular,Typescript,我有一个角度组件,我正在向构造函数中的表单添加一些验证器。但是,我想在我的ngOnit方法中添加额外的验证器。我怎样才能做到这一点 export class ResetPasswordComponent implements OnInit { constructor( private fb: FormBuilder, private route: ActivatedRoute, private forgotPasswordServ
export class ResetPasswordComponent implements OnInit {
constructor(
private fb: FormBuilder,
private route: ActivatedRoute,
private forgotPasswordService: ForgotPasswordService,
private configService: ConfigService,
private usersService: UsersService
) {
this.blacklistedPasswords = this.configService.config.blacklistedPasswords;
this.formGroup = this.fb.group(
{
password: ['', [Validators.required, Validators.minLength(8)]],
confirmPassword: ['', [Validators.required, Validators.minLength(8)]]
},
{
validator: Validators.compose([
passwordStrengthValidator('password', 'confirmPassword'),
blacklistedPasswordValidator(
'password',
'confirmPassword',
this.blacklistedPasswords
)
])
}
);
}
ngOnInit() {
}
}
如何将另一个验证器附加到组件Ngonit方法?
例如,我想添加一个与validator('password','confirmPassword')匹配的验证器,类似于passwordStrengthValidator&blacklistedPasswordValidator。我怎样才能做到呢
我是一个新的bie,非常感谢您因此,您可以像这样附加新的验证器:
ngOnInit() {
this.formGroup.setValidators(Validators.compose([this.formGroup.validator, matchingValidator('password', 'confirmPassword')]));
}
您需要在此处传递所有现有验证器,因为setValidators清除所有现有验证器,然后添加您传递的内容。希望对您有所帮助。您想为密码创建自定义验证程序吗?我已经创建了一个名为matchingValidator的验证程序,我想将该验证程序附加到passwordStrengthValidator和blacklistedPasswordValidator的列表中,但是在ngOnInit方法中,因为我获取了一些对象并将其传递给我的matchingValidator。我不明白你的意思。你可以添加一些代码吗?我想你可以使用setValidators方法。类似这样的东西。yourFormGroup.setValidators(匹配验证器);我试图实现的是在ngonit方法中添加新的验证器,而不丢失以前在构造函数方法中添加到表单中的验证器。非常感谢。