gpt4 book ai didi

Angular 6 : Custom validators not executing

转载 作者:太空狗 更新时间:2023-10-29 18:12:29 25 4
gpt4 key购买 nike

我已经为此奋斗了 2 天。我只是不知道出了什么问题。似乎我的自定义验证器没有执行。

如果您只需按“下一步”按钮,每个控件都会以红色边框突出显示。如果您填写表格,在电子邮件字段中键入不同的值,然后按“下一步”,则表格有效,并且我没有看到来自自定义电子邮件验证器的任何 console.log。

https://stackblitz.com/edit/angular-nwhgpb

最佳答案

(IF you want to excute passwordValidator you need to add '()' to your Validator Function example password1: new FormControl("",[Validators.required,this.passwordValidator()]), Even though it will throw error because FormGroup you used inside will not have a reference to the formData password1 and password2)

如果你想比较两个输入字段,你需要在 formGroup 中使用 formGroup,因为如果你不使用 formGroup,formGroup 将不会持有这样的对象。

value: Object
email1: "data"
email2: ""
__proto__: Object

https://angular.io/guide/form-validation#custom-validators使用此 Ref 实现比较密码和电子邮件

/** A hero's name can't match the given regular expression */
export function forbiddenNameValidator(nameRe: RegExp): ValidatorFn {
return (control: AbstractControl): {[key: string]: any} | null => {
const forbidden = nameRe.test(control.value);
return forbidden ? {'forbiddenName': {value: control.value}} : null;
};

关于 Angular 6 : Custom validators not executing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51556469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com