K4mczi
K4mczi

Reputation: 397

Is it possible to get minlength value from formcontrol?

So i have my form validation. And my question is can i get for example a minlength value which i passed at creating formControl?

I havent found any information.

This is my dumb component and i want to get minlength value to pass to information. Now i need to pass it via @Input().

title: ['', [Validators.required, Validators.minLength(4), Validators.maxLength(20)]]

.

<div class="validation-window">
  <p *ngIf="errors.required">
    {{field}} required
  </p>
  <p *ngIf="formControl.hasError('minlength')">
      {{field}} at least {{minLegth}} characters
  </p>
  <p *ngIf="formControl.hasError('maxlength')">
      {{field}} at least {{maxLength}} characters
  </p>
</div>

I want to replace {{maxLength}} with something like formControl.validators.minlength.value;

Upvotes: 12

Views: 8468

Answers (1)

AVJT82
AVJT82

Reputation: 73357

Yes, you can access the number of the length, both in maxlength and minlength. You can find it inside the error object, where it lies under .maxlength.requiredLength && minlength.requiredLength. There is also a field with actualLength, but you don't seem to need it, but if you sometimes do! :)

<p *ngIf="formControl.hasError('maxlength')">
  {{field}} at max {{formControl.errors.maxlength.requiredLength}} characters
</p>

<p *ngIf="formControl.hasError('minlength')">
  {{field}} at least {{formControl.errors.minlength.requiredLength}} characters
</p>

DEMO

Upvotes: 17

Related Questions