In Angular 4 app I have a form model like this:
this.form = this._fb.group({
title: ['', [Validators.required, Validators.minLength(3), Validators.maxLength(50)]],
description: ['', [Validators.required, Validators.minLength(3)]]
});
Now what I want is to remove dynamically only the required validator from the control validators array. Something like this:
saveDraft() {
this.form.controls['title'].removeValidator('required'); //Just a fake implementation for demonstration
}
This question is not the duplicate of the mentioned question. My case is different I just want to remove the required validator unknowingly the other ones.
I had the same problem with saving entry as draft and prepared the following solution:
if you want to add validation try this one.
if you want to remove validators try this one.
Unfortunately Angular doesn't have a removeValidator capability at this point in time. All you can do is re-set the validators without the one you want to remove, so you need to know which validators you want to keep rather than which one you want to remove. so this:
Is the closest you have to a remove function. You can't access the current validators on a formcontrol either to try and write your own remove function. The best you could do is write your own form control validation manager class that can keep track of the validators on a given control and manage them for you.
Use empty array to remove all existing validators.
we can use setValidators to remove validation.