4
<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

1
  • Please add the component .ts code to your question. Commented Jan 7, 2020 at 11:28

4 Answers 4

4

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)]], 
Sign up to request clarification or add additional context in comments.

5 Comments

when I start typing something in the input field it's giving me an error "TypeError: Cannot read property 'maxlength' of null at Object.eval"
if you want use errors.maxlength, don't forget the "safe operator" errors?.maxlength
@Emilien Yes, it does now after I modified the custom validation method. Thank you.
@IvilinStoyanov If your solution come from StackOverflow, please make an answer as "resolved". Otherwise, you can edit your post adding your solution :)
@Emilien Okay :)
1

The problem was in my custom validation method where I remove any errors for the following form control. I forgot about this method. Thank you all for the help.

validateCustomerDetails(g: FormGroup) {
if ((isNullOrUndefined(g.get('registration').value) || g.get('registration').value == "") &&
  (isNullOrUndefined(g.get('customer_surname').value) || g.get('customer_surname').value == "")) {
  g.controls['registration'].setErrors({ 'empty': true });
  g.controls['customer_surname'].setErrors({ 'empty': true });
}
else {
  g.controls['registration'].setErrors(null);
  g.controls['customer_surname'].setErrors(null);
}

Comments

0
  *ngIf="addLockerForm.controls?.registration?.invalid && addLockerForm.controls?.registration?.touched"

this.addLockerForm = formbuilder.group({
     registration: [
       "",
       [
         Validators.required,
         Validators.minLength(4),
         Validators.maxLength(8),
         Validators.pattern(Your Pattern)
       ]
     ]  
  }

If U are Using FormBuilder And Validator Use FormBuilder In Your Ts File

Comments

0

try following code

<mat-form-field class="input-label-add">
   <input matInput placeholder="Registration **" formControlName="registration">
   <div *ngIf="registration.errors">
      <mat-error *ngIf="addLockerForm.get('registration').errors.maxlength">
         registration cannot exceed 8 characters</mat-error>
      <mat-error *ngIf="addLockerForm.get('registration').errors.required">registration or surname is required</mat-error>
   </div>
</mat-form-field>

1 Comment

Akhil, if you want use errors.maxlength, don't forget the "safe operator" errors?.maxlength

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.