2016-05-13 13 views
1

部分的にモデル駆動型のテンプレート駆動型フォームがあり、カスタムロジックを使用して入力の1つを検証したいと考えています。問題は、検証が有効であってもコントロールが無効な状態にあることです。私は間違って何をしていますか?検証サービスを使用した検証

<span *ngIf="identityNumber.touched && !identityNumber.valid">invalid</span> 

..

this.form = this.formBuilder.group({ 
    identityNumber: ['', Validators.required, 
     ValidationService.validateIdentityNumber] 
}); 

..

public static validateIdentityNumber(control: Control) { 
    let val: string = <string>control.value; 
    if (val && val.length == 11) { 
    return null; 
    } else { 
    return { "invalidIdentityNumber": true }; 
    } 
} 

http://plnkr.co/edit/1IY4iXEeQDXUanCOSCKX?p=preview

それはあなたが11文字を入力しても "無効" として表示されます。

(私は長さバリデータがあります知っている、これはサンプルのためである)

答えて

0

あなたはValidators.compose

identityNumber: ['', 
    Validators.compose([ 
     Validators.required, 
     ValidationService.validateIdentityNumber 
    ]) 
] 

つ以上のバリデータの使用を渡したい場合は(3番目のパラメータは、非同期バリデータ用です)

関連する問題