<mat-form-field class="input-label-add">
<input matInput placeholder="Registration **" formControlName="registration">
<mat-error *ngIf="addLockerForm.get('registration').hasError('maxlength')">registration cannot exceed 8 characters</mat-error>
<mat-error *ngIf="addLockerForm.get('registration').errors">registration or surname is required</mat-error>
</mat-form-field>
this.addLockerForm = this.formBuilder.group({
locker_serial_number: [null, Validators.required],
customer_surname: [null],
registration: [null, Validators.maxLength(10)],
mobile: [null],
email: [null],
date_in: [null, Validators.required],
date_out: [null, Validators.required],
},
{ validator: [this.validateCustomerDetails, this.validateCustomerContact] });
addLockerForm.get('registration').hasError('maxlength') stays false all the time
Could you try this ?
addLockerForm.get('registration').errors.maxlength
https://angular.io/guide/form-validation#validator-functions
EDIT 1
You don't need to use .errors
.
Please update your code like this :
<mat-form-field class="input-label-add">
<input matInput placeholder="Registration **" formControlName="registration">
<mat-error *ngIf="addLockerForm.get('registration').hasError('maxlength')">registration cannot exceed 8 characters</mat-error>
<mat-error *ngIf="addLockerForm.get('registration').hasError('required')">registration or surname is required</mat-error>
</mat-form-field>
registration: [null, [Validators.required, Validators.maxLength(8)]],