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]
});
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();
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