d-kostov-dev / ng2-mdf-validation-messages

Angular 2 Model Driven Forms Validation Messages
MIT License
11 stars 8 forks source link

Passwords validation broken #9

Closed shayanhusaini closed 7 years ago

shayanhusaini commented 7 years ago

Hi, I am using your extension for my project. My application broke when i use the passwords validation method in reset password page. Below is my code:

@Component({
  selector: 'app-resetpassword',
  templateUrl: './resetpassword.component.html',
  styleUrls: ['./resetpassword.component.css']
})
export class ResetpasswordComponent implements OnInit {
  public resetPasswordForm: FormGroup;
  public resetToken: string = '';
  public sub: any = {};

  constructor(private _fb: FormBuilder, private route: ActivatedRoute, private router: Router) { }

  ngOnInit() {
    this.sub = this.route.queryParams.subscribe(params => {
        this.resetToken = params['resetToken'] || '';
      });
    this.initializeForm();
  }

  save(data, valid) {}

  ngOnDestroy() {
    this.sub.unsubscribe();
  }

  private initializeForm() {

    this.resetPasswordForm = this._fb.group({
      password: ['', [ValidationExtensions.required(), ValidationExtensions.passwords()]],
      confirm_password: ['', [ValidationExtensions.required(), ValidationExtensions.passwords()]]
    });
  }

}

I got the following error: error-passwords

d-kostov-dev commented 7 years ago

Hi shayanhusaini.

The passwords validation should be used on a group not on control.

         this.password = this.formBuilder.control('', [
            ValidationExtensions.required('Password is required.'),
            ValidationExtensions.minLength(3, 'Password must be more than 3 symbols.'),
            ValidationExtensions.maxLength(20, 'Password must be less than 20 symbols.'),
        ]);

        this.confirmPassword = this.formBuilder.control('', [
            ValidationExtensions.required('Confirm Password is required.'),
            ValidationExtensions.minLength(3, 'Password must be more than 3 symbols.'),
            ValidationExtensions.maxLength(20, 'Password must be less than 20 symbols.'),
        ]);

        // VALIDATION
        this.passwordsGroup = this.formBuilder.group({
            password: this.password,
            confirmPassword: this.confirmPassword,
        }, { validator: ValidationExtensions.passwords() });

You can check the demo app here: https://github.com/d-kostov-dev/ng2-mdf-validation-messages/blob/master/demo-app/src/app.component.ts