Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 get existing validators of a formcontrol

Lets say

surname = new FormControl('', [Validators.required, Validators.minLength(2)]); 

At some point depending on the situation I may add or delete any validators on surname control.

At the end How do I know what validators exists on surname control? I couldn't find any thing in documentation nor by dumping the control into console

Something like

surname.getValidators() should return - ['required', 'minLength'] 
like image 853
krishna Avatar asked Apr 06 '17 13:04

krishna


People also ask

How do you add validators in FormControl?

We can add Validators dynamically using the SetValidators or SetAsyncValidators. This method is available to FormControl, FormGroup & FormArray. There are many use cases where it is required to add/remove validators dynamically to a FormControl or FormGroup.

How do I get FormControl values?

To fetch the value of a form control, we have to use value property on the instance of FormControl in our class. In the same way we can fetch the value in HTML template. city = new FormControl('Noida'); console.

Which method is used to create an array of validators on a FormControl?

FormArraylink. Tracks the value and validity state of an array of FormControl , FormGroup or FormArray instances.

Is it possible to define validators in form builder?

FormBuilder allows us to explicitly declare forms in our components. This allows us to also explicitly list each form control's validators. In our example we are going to build a small form with three inputs, user name, email and profile description. We will start with looking at our app.


1 Answers

Reading validators from a control is currently not supported

See also https://github.com/angular/angular/issues/13461

like image 163
Günter Zöchbauer Avatar answered Sep 22 '22 14:09

Günter Zöchbauer