Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;

like image 792
K4mczi Avatar asked May 03 '19 18:05

K4mczi


People also ask

What is the option available to change the value in FormControl?

setValue() examples. The FormControl. setValue() sets a new value to this control.

What is the first parameter in New FormControl ()?

The first parameter, is the FormState, in which we can set the Form Control initial value and if it should be disabled initially.

What is validators in Angular?

A validator is a function that processes a FormControl or collection of controls and returns an error map or null. A null map means that validation has passed.


1 Answers

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

like image 177
AT82 Avatar answered Oct 11 '22 09:10

AT82