I have a formcontrol where I want to change the color when the field is invalid I have tried the following as most examples do the same:
<input
formControlName="personNameField"
type="text"
placeholder="Bitte eingeben"
[ngClass]="{'error': personNameField.errors}"
></input>
My ts formcontrol is generated like this:
form = this.builder.group({
personNameField: new FormControl('',
[Validators.required]),
});
getName(){
this.form.get('personNameField')
}
But I am getting the following error:
ERROR TypeError: Cannot read properties of undefined (reading 'errors')
any idea what I am doing wrong?
UPDATE: I added the getter and removed the question mark but still the bordering does not work only error message is shown.
UPdate2:
.error {
// underline input field on error
border: 1px solid red;
display: block;
color: red;
}