2017-01-28 11 views
1

電子メールが存在するかどうかをチェックしてエラーを返すことでhttp検証を実行するフォームがあります。 _authserviceで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}; 
     } 
     }); 
    } 
} 

でフォームコンポーネント

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 
    ])] 
}) 

(そのサービス)

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; }'.) 

もっと必要なことは何ですか?

+0

が関連している可能性がある。https://github.com/angular/angular/issues/9336。これには新しいカスタム検証の例が含まれています:http://plnkr.co/edit/8DsRqpHo4tH4MPYauWAu?p=preview – Set

答えて

2

https://angular.io/docs/ts/latest/api/forms/index/FormBuilder-class.html

非同期バリ3パラメータ

email:['', [Validators.required], [ 
    this._validationService.emailExistsValidator.bind(this) 
]] 
+0

は、厄介なパラメータとして追加した後でもエラーを返します。 –

+0

エラーが読み込まれます。未定義の '_authService' –

+0

エラーは型 '((control:AbstractControl)=> void)の引数にシフトします。[]'は 'ValidatorFn []'型のパラメータに代入できません。 タイプ '(Control:AbstractControl)=>' void 'は' ValidatorFn 'タイプに割り当てられません。 タイプ 'void'はタイプ '{[key:string]:any; } ')。 –

関連する問題