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;
setValue() examples. The FormControl. setValue() sets a new value to this control.
The first parameter, is the FormState, in which we can set the Form Control initial value and if it should be disabled initially.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With