Required Validator is not removed by required directive

This issue has been tracked since 2022-09-19.

Which @angular/* package(s) are the source of the bug?

forms

Is this a regression?

No

Description

When setting up a control with required validator (eg: new FormControl('', Validators.Required) ), the required directive [required] does not have effect.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-ivy-cuadg4

Please provide the exception or error you saw

I would expect the changes in the value of the "required" directive would be aligned with the control validators.

Please provide the environment you discovered this bug in (run ng version)

I found that issue in angular 12 but seems to be also happening in angular 14.2

Anything else?

In the minimal reproduction we can see three controls.
First one with initial required validator plus a custom isRequired directive which acts as I would expect.
Second one without initial required validator plus required directive which acts as I would expect.
Third one with initial required validator plus required directive which required gets miss aligned.

geromegrignon wrote this answer on 2022-09-19

As explained in the Documentation: https://angular.io/guide/form-validation#validating-input-in-reactive-forms

In a reactive form, the source of truth is the component class. Instead of adding validators through attributes in the template, you add validator functions directly to the form control model in the component class.

If you need to change the required attribute, update the control's Validators on the component side.

angelfraga wrote this answer on 2022-09-19

Thanks.

I will then remove the data binding to the required attribute in the template and apply the changes directly to the control validators.

More Details About Repo
Owner Name angular
Repo Name angular
Full Name angular/angular
Language TypeScript
Created Date 2014-09-18
Updated Date 2022-10-05
Star Count 84147
Watcher Count 3063
Fork Count 22247
Issue Count 1201

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date