I am seeking help to understand how to write a custom validator for a reactive form.
Here is the component code:
private form: FormGroup;
ngOnInit() {
const this_ = this;
this.form = new FormGroup({
'email': new FormControl(null, [Validators.required, Validators.email]),
'password': new FormControl(null, [Validators.required, Validators.minLength(6)]),
'password2': new FormControl(null, [Validators.required, Validators.minLength(6), this_.comparePasswords]),
'name': new FormControl(null, [Validators.required]),
'agree': new FormControl(false, [Validators.requiredTrue])
});
}
comparePasswords(c: FormControl) {
console.log(c);
const hashStr = Md5.hashStr(c.value.password);
const hashStr2 = Md5.hashStr(c.value.password2);
console.log(hashStr, hashStr2);
return (hashStr === hashStr2) ? null : {
comparePasswords: {
valid: false
}
};
}
All necessary imports are included. Upon page load, the browser console immediately displays the form object with null values.
I am facing an issue in implementing the check within the function comparePasswords().
Error message displayed on console:
ERROR TypeError: Cannot read property 'password' of null