所以这是我的麻烦,我正在验证电子邮件,我想有点宽容(取决于电子邮件应该绝对完美或不完美),为此我使用带有参数的自定义验证器(这将触发排除可疑电子邮件)所以这是我的代码:
组件:
FormEmailAddress : FormGroup;
constructor(private fb: FormBuilder) { }
ngOnInit(){
this.FormEmailAddress = this.fb.group({
emailAddress: ['', Email.validateEmail]
});
}
验证者:
static validateEmail(control, withsuspicious) {
if (Validators.required(control)!== undefined && Validators.required(control)!== null) return null;
return Email.checkValidity(control.value, withsuspicious)? null : { pattern: {invalid: true}}
}
Email.checkValidity只是一个验证电子邮件的函数,如果好则返回true,否则返回false(使用regexp等)
查看
<form [formGroup]="FormEmailAddress" novalidate>
<h4>Email Model Driven</h4>
<input type="text" formControlName="emailAddress"/>
<p>Email : {{FormEmailAddress.get('emailAddress').value}}</p>
<p *ngIf="!FormEmailAddress.valid"> Wrong Email </p>
</form>
所以我想找到一种方法来传递,以及我的“emailAddr”参数withsuspicious到我的验证器中你可以帮助我PLZ吗?感谢
答案 0 :(得分:0)
终于找到了怎么做,没有更改视图或验证器中的任何内容,juste在组件上做了这个:
this.FormEmailAddress = this.fb.group({
emailAddress: [''],
withsuspicious: [true]
}, {validator : this.validateEmail('emailAddress', 'withsuspicious')});
validateEmail(emailAddressKey, withsuspiciousKey){
return(group : FormGroup) => {
let emailInput = group.controls[emailAddressKey]
let withsuspiciousInput : boolean = group.controls[withsuspiciousKey].value
return Email.validateEmail(emailInput, withsuspiciousInput)
}
}