Reputation: 935
I need to get the value of the username control and the password control to compare them.
Here is my custom validator
import { FormControl } from '@angular/forms';
export class PasswordValidator {
static isNotEqual(control: FormControl) {
if (control.get('username').touched && control.get('password').touched) {
if (control.get('username').value != control.get('password').value) {
return { isNotEqual: true }
} else {
return { isNotEqual: false }
}
} else {
}
}
}
The problem here is that I cannot get the value of the two controls. Anyone help?
Upvotes: 1
Views: 1829
Reputation: 5532
You can access second control by parent:
isEqual(c: FormControl): any {
if (c.parent) {
if (c.parent.value['username'] !== c.value) {
return {isNotEqual: true}
} else {
return {isNotEqual: false}
}
}
return null;
}
You have to add this validator to the password form control, or opposite.
Upvotes: 3