Custom Validator on reactive form for password and confirm password matching getting undefined parameters into Angular 4

后端 未结 9 1372
遥遥无期
遥遥无期 2020-12-01 00:47

I\'m trying to implement a custom validator to check if the password and password confirm are equal. The problem is that the validator is getting undefined password and conf

9条回答
  •  庸人自扰
    2020-12-01 01:24

    import {AbstractControl, FormBuilder, FormGroup, Validators} from

    set your password input into the group and no need to use "ngModel".

    Debe ingresar una contraseña

    Password must be required

    password does not match

         buildForm(): void {
                this.userForm = this.formBuilder.group({
                    passwords: this.formBuilder.group({
                        password: ['', [Validators.required]],
                        confirm_password: ['', [Validators.required]],
                    }, {validator: this.passwordConfirming}),
    
                });
            }
    

    add this custom function for validate password and confirm password

      passwordConfirming(c: AbstractControl): { invalid: boolean } {
        if (c.get('password').value !== c.get('confirm_password').value) {
            return {invalid: true};
        }
    }
    

    Display error when password does not match

    Password does not match

提交回复
热议问题