Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Validator to required if visible?

I am wondering how I can set the Validator in the following form to Required, only if the form element is present:

<div *ngIf="form.controls.user.value !== 'Admin' && form.controls.user.value ">
        <label>Role:</label>
        <input type="text" ngControl="role">
</div>

And my form is:

       this.form = this._formBuilder.group({
        user: ['',Validators.required],
        role: ['', Validators.required]
    });
like image 925
uksz Avatar asked Mar 17 '16 10:03

uksz


1 Answers

You can use the disable() and enable() functions. When a form control is disabled, validation is not applied to that control.

this.form.controls.user.disable();
this.form.controls.role.disable();
like image 132
Christian Avatar answered Oct 12 '22 12:10

Christian