Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if input control has certain type of vallidator in angular2

I have component that wraps input field. In the component i receive the Control object from @Input() inputControl: Control;. In the template i have span that shows message if input field in the component is not required.

<span
  class="input-label-caption">
  (optional)
</span>

and the input

<input
    *ngIf="inputMode=='text' || inputMode=='email'"
    type="{{inputMode}}"
    [ngFormControl]="inputControl"
    placeholder="{{placeholder}}"
    class="input-text"
    [disabled]="inputDisabled"
    [ngClass]="{
    'inverted': inverted
    }">

How can i read form inputControl object if it contains Validators.required? I want to know if i can used it like this for example

<span
  class="input-label-caption"
  *ngIf="!inputControl.validators.required"
  >
  (optional)
</span>
like image 241
Kliment Avatar asked Jan 18 '16 16:01

Kliment


People also ask

How do you check if a FormControl is required?

validator({} as AbstractControl); This will return an Object with the list of validators that are present on your FormControl . You can then check for the required key in the Object. If it exists and its value is true then you can be sure that a Required Validator is applied on the FormControl .

What are the types of validator functions in Angular?

Angular provides some handy built-in validators which can be used for reactive forms and template-driven forms. Most known validators are required , requiredTrue , min , max , minLength , maxLength and pattern .


2 Answers

You can try to use this expression:

<span
  class="input-label-caption"
  *ngIf="!inputControl.errors?.required"
>
  (optional)
</span>

The errors attribute contains one entry per name of validator that failed.

I use the Elvis operators for the errors attribute since it can be undefined if there is no validation error.

Edit

Following your comment, I think that you can check a "required" validator using the === operator with the Validators.required function directly. In fact a validator is simply a function and the Validators.required function is used for "required" validation.

Here is the corresponding code:

this.hasRequiredValidator = (this.inputControl.validator === Validators.required);

In the case where the validator attribute is an array, you need to extend a bit the test to check if the Validators.required function is present in the array.

Now the code in the template would be:

(optional)

Hope it helps you, Thierry

like image 192
Thierry Templier Avatar answered Oct 11 '22 14:10

Thierry Templier


I couldn't get the above to work which isn't surprising given the changes to Angular since Jan. With the latest version of Angular (2.2.0)you will need something like this in your class.

  get required(): boolean { 
    var _validator: any = this.inputControl.validator && this.inputControl.validator(this.inputControl);
    return _validator && _validator.required;
  }

This will also handle the case where you have multiple validators in a reactive form e.g.

      name: ['', [Validators.required, Validators.minLength(2)]]
like image 42
Fairlie Agile Avatar answered Oct 11 '22 14:10

Fairlie Agile