2017-06-27 11 views
2

ここにはdemo plnkrがあります。私は、テキストボックスと確認ボタンを持つOTP入力用のカスタム非同期バリデータを実装しようとしています。ユーザーがOTPの検証ボタンまたはフォーム送信ボタンをクリックしたときにのみ入力を検証したいと思います。現在、テキストチェンジイベントで検証が行われており、動作していません。 フォームHTML:角度カスタム非同期バリデーターが機能しない

<form [formGroup]="registrationForm" (ngSubmit)="registrationForm.valid && submitRegistration(registrationForm.value)" novalidate> 
    <fieldset class="form-group"> 
    <label for="e-mail">Mobile</label> 
    <div class="input-group"> 
     <input type="text" class="form-control" placeholder="Mobile" formControlName="mobile"> 
     <span class="input-group-btn"> 
     <button class="btn btn-secondary" type="button">Send OTP</button> 
     </span> 
    </div> 
    <div class="form-text error" *ngIf="registrationForm.controls.mobile.touched"> 
     <div *ngIf="registrationForm.controls.mobile.hasError('required')">Mobile is required.</div> 
    </div> 
    </fieldset> 
    <fieldset class="form-group"> 
    <label for="e-mail">Verify OTP</label> 
    <div class="input-group"> 
     <input type="text" class="form-control" placeholder="OTP" formControlName="otp"> 
     <span class="input-group-btn"> 
     <button class="btn btn-secondary" (click)="veryOTPAsyn(otp)" type="button">Verify</button> 
     </span> 
    </div> 
    <div class="form-text error" *ngIf="registrationForm.controls.otp.touched"> 
     <div *ngIf="registrationForm.controls.otp.hasError('required')">OTP is required.</div> 
     <div *ngIf="registrationForm.controls.otp.hasError('invalidOtp')">OTP is invalid.</div> 
    </div> 
    </fieldset> 
    <button class='btn btn-primary' type='submit' [disabled]='!registrationForm.valid'>Submit Registration Form</button> 
</form> 

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

export class ExampleFormComponent { 
    registrationForm: FormGroup; 

    constructor(public fb: FormBuilder) { 
    // Example use of FormBuilder, FormGroups, and FormControls 
    this.registrationForm = fb.group({ 
     mobile: ['', Validators.required], 
     otp: ['', Validators.compose([Validators.required, this.veryOTPAsyn.bind(this)])], 
     dob: ['', Validators.required], 
     email: ['', Validators.compose([Validators.required, emailValidator])], 
     password: ['', Validators.required], 
     confirmPassword: ['', Validators.required], 
     firstName: ['', Validators.required], 
     lastName: ['', Validators.required] 
    }, {validator: matchingPasswords('password', 'confirmPassword')}) 

    } 

    submitRegistration(value: Object): void { 
    console.log(value); 
    } 

    veryOTPAsyn(otpControl: FormControl): Promise<any> { 
    console.log(otpControl) 
    console.log(otpControl.hasError('invalidOtp')) 
    return new Promise<any>(
     (resolve, reject) => { 
     setTimeout(() => { 
      resolve({invalidOtp:true}); 
     }, 500); 
     }); 
    } 

} 

答えて

1

を行きますカスタムバリデータを削除する代わりに、コンポーネントが初期化されたときにカスタムバリデータが実行されるため、また入力値が変更されるたびにユーザーがボタンをクリックしたときにエラーを設定/削除します。だからあなたのクリックイベントがあり、この(擬似コード)のような何かを:

(click)="veryOTPAsyn(registrationForm.controls.otp)" 

とTS:PLUNKERフォーク状

veryOTPAsyn(ctrl: FormControl) { 
    if(ctrl.value == 'hey') { 
    ctrl.setErrors({'invalidOtp':true}) 
    } else { 
    ctrl.setErrors({'invalidOtp':null}) 
    } 
} 

を。


EDIT:

しかし、あなたは非同期バリデータを使用してのルートを移動したい場合は、ちょうど3番目の引数としてバリデータを追加したが、その後、あなたが「検証」ボタンを必要としません。

otp: ['', [Validators.required], [this.veryOTPAsyn]] 

またはそれがV 5.0.0

otp: ['', {validators: [Validators.required], asyncValidators: [this.veryOTPAsyn]}] 

PLUNKERのリリース後に今行われているように(前v.5.0.0、非同期バリデータをマーキングの最初のオプションが使用されているので)それを行う

1

はこれを試してみてください:

otp: ['', Validators.required, this.veryOTPAsyn.bind(this)] 

非同期バリデータは、私がconsideう第三引数に

関連する問題