与服务器进行表单验证 [英] Form validation with the server
本文介绍了与服务器进行表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表格,我想通过检查电子邮件是否存在但返回错误来执行http验证.这是我的表格:
I have a form that I would like to perform http validation by checking if an email exists but it returns an error. This is my form:
在表单组件上
constructor(
private _formBuilder:FormBuilder,
private _validationService:ValidationService
) { }
ngOnInit() {
this.resetForm = this._formBuilder.group({
email:['',Validators.compose([
this._validationService.emailExistsValidator //this returns an error
,Validators.required
])]
})
在validationService上
On the validationService
constructor(
public _authService:AuthService //tried also with private
){}
emailExistsValidator(control){
if(control.value != undefined) {
this._authService.checkExists("email")
.map(response => {
if (!response) {
return {'emailNotExists': true};
}
});
}
}
在_authservice(其服务)上
On the _authservice (its a service)
checkExists(value:string):Observable<any>{
return this._httpclient.get(this.authurl+value) //httpclient attches the headers
.map(response => {
return response
});
}
现在出现此错误
Argument of type '((control: any) => void)[]' is not assignable to4
parameter of type 'ValidatorFn[]'.
Type '(control: any) => void' is not assignable to type 'ValidatorFn'.
Type 'void' is not assignable to type '{ [key: string]: any; }'.)
我需要做些什么?
推荐答案
https://angular.io/docs/ts/latest/api/forms/index/FormBuilder-class.html
异步验证器是第三个参数
async validators is the 3rd parameter
email:['', [Validators.required], [
this._validationService.emailExistsValidator.bind(this)
]]
这篇关于与服务器进行表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文