2017-07-31 36 views
0

登録フォームにpasswordconfirm_passwordフィールドがあります。これらのパスワードが同じであることを確認するカスタム検証があります。事は:passwordフィールドに「qwerty123」と入力し、password_confirmフィールドにはqwerty123と入力しても問題ありません。しかし、たとえばフィールドに4のような文字を追加してから、passwordフィールドに同じ文字4を追加すると、フォームは有効ではありません(プロパティvalidfalseです)、何もできません。

私はここで似たような解決策を見ましたが、そこに役立つものは私には役に立ちません。

マイコンポーネント:
角型カスタム検証

public userNameInput: FormControl = new FormControl('', [ 
    Validators.minLength(this.limits['username'][0]), 
    Validators.maxLength(this.limits['username'][1]) 
    ]); 
    public emailInput: FormControl = new FormControl('', [ 
    Validators.required, 
    RegisterFormComponent.checkEmail 
    ]); 
    public passwordInput: FormControl = new FormControl('', [ 
    Validators.required, 
    Validators.minLength(this.limits['password'][0]), 
    Validators.maxLength(this.limits['password'][1]), 
    RegisterFormComponent.checkPasswordsMatching 
    ]); 
    public confirmPasswordInput: FormControl = new FormControl('', [ 
    Validators.required, 
    RegisterFormComponent.checkPasswordsMatching 
    ]); 

    public registrationForm: FormGroup = this.formBuilder.group({ 
    userName: this.userNameInput, 
    email: this.emailInput, 
    password: this.passwordInput, 
    confirmPassword: this.confirmPasswordInput 
    }); 

    private static checkPasswordsMatching(input: FormControl): null | { [ key: string ]: boolean } { 
    if (!input.root || !input.root.get('password')) { 
     return null; 
    } 

    return (
     (
     input.root.get('password').value === '' || 
     input.root.get('confirmPassword').value === '' 
    ) 
     || 
     input.root.get('password').value === 
     input.root.get('confirmPassword').value 
    ) 
     ? null 
     : { mismatched: true }; 
    } 

テンプレートからの私のHTML:

<input 
    type="text" 
    name="username" 
    id="username" 
    [formControl]="userNameInput" 
    [class.error]=" 
    userNameInput.hasError('minlength') || 
    userNameInput.hasError('maxlength') 
    " 
> 
<input 
    id="email" 
    type="text" 
    name="email" 
    [formControl]="emailInput" 
    [class.error]=" 
    !emailInput.pristine && 
    emailInput.hasError('invalid') 
    " 
> 
<input 
    type="password" 
    name="password" 
    id="password" 
    [formControl]="passwordInput" 
    [class.error]=" 
    passwordInput.hasError('minlength') || 
    passwordInput.hasError('maxlength') || 
    confirmPasswordInput.hasError('mismatched') 
    " 
> 
<input 
    type="password" 
    name="password_confirm" 
    id="password_confirm" 
    [formControl]="confirmPasswordInput" 
    [class.error]=" 
    passwordInput.hasError('mismatched') || 
    confirmPasswordInput.hasError('mismatched') 
    " 
> 
<button 
    [disabled]="!registrationForm.valid" 
>Confirm</button> 
+0

どのエラーが表示されているのを確認しましたか? 「パスワード」フィールドが長すぎる可能性がありますか?そこに最大長の検証がありますが、 'confirmPasswordInput'のための検証はありません – Ludevik

+0

いいえ、それは 'ミスマッチ'エラーです。それは確かに長さではありません。 – WeekendMan

答えて

1

角度が一人のユーザが現在に入力しているだけで、他の入力にバリデータを再実行していないためです。他の入力のバリデーターをupdateValueAndValidityで再実行することができます。

+1

はい、私は全体の画像を見つけました。 1.あなたが言ったように、Angularはバリデータを更新しません。私は手動で行う必要がありますが、時にはその理由がわかりません。 2.重要 - FormGroupを使用して2つのパスワード入力をグループ化し、一致するバリデータをこのグループに設定する必要があります。このパスワードグループは、メインフォームFormGroupのグループでなければなりません。 詳細についてはこちらをご覧ください: https://bertrandg.github.io/angular2-forms-form-group-validation/ 同じ問題が発生する可能性のあるすべての人に幸運。 – WeekendMan