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

后端 未结 9 1387
遥遥无期
遥遥无期 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:33

    this.myForm = this.fb.group({
        userEmail: [null, [Validators.required, Validators.email]],
        pwd: [null, [Validators.required, Validators.minLength(8)]],
        pwdConfirm: [null, [Validators.required]],
    }, {validator: this.pwdConfirming('pwd', 'pwdConfirm')});
    
    
    
    pwdConfirming(key: string, confirmationKey: string) {
        return (group: FormGroup) => {
            const input = group.controls[key];
            const confirmationInput = group.controls[confirmationKey];
            return confirmationInput.setErrors(
                input.value !== confirmationInput.value ? {notEquivalent: true} : null
            );
        };
    }
    

提交回复
热议问题