Help please understand. I'm trying to write a custom validator for a reactive form.
component:
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 the imports that you need to connect. After loading the page, the browser console immediately displays the form object, in which the value object is null.
I can not make check in the function comparePasswords().
Console display follow:
ERROR TypeError: Cannot read property 'password' of null
""
? I bet that's the issue.