The `setErrors` method does not set errors
See original GitHub issueWhen you set the errors of a FormControl
(using the setErrors
method) inside a FormGroup
, where the FormControl
contains validators then the errors are not set.
formGroup: FormGroup;
constructor(private formBuilder: FormBuilder) {
this.formGroup = this.formBuilder.group({
login: ["", [Validators.required]]
});
}
ngOnInit() {
const formLoginControl = this.formGroup.controls.login;
formLoginControl.setErrors({ incorrect: true });
}
Here is a stackblitz of the issue.
This is happening because the validation has not run yet. Since setErrors
is called before the validation has run, the errors get overwritten by the validator. Note that this issue occurs, when your FormControl
is part of a FormGroup
- it does not happen on stand-alone controls that are not part of a FormGroup
.
To overcome this, setErrors
needs to be called in a setTimeout
.
Action items:
- Update the docs to make this issue clear
- Fix the underlying issue
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Angular: setErrors not displaying message - Stack Overflow
I get this error" Cannot read property 'setErrors' of null"..keep in mind c.value does return the correct value for the phone control.. –...
Read more >The FormControl.errors field is empty after setErrors is used to ...
In my example I use angular form and method 'setErrors' for control. But when I use dx-number-box property 'errors' is always null. I...
Read more >setErrors() - React Advanced Form - GitBook
setErrors (). Applies the given error messages to the selected fields. Accepts the delta fields object, where each field path is associated with...
Read more >FormGroup - Angular
This method does not update the value or validity of the control. ... It accepts both super-sets and sub-sets of the group without...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@all
I was also facing the same issue but I think it should be resolved as per following:
It works for me properly with expected output whenever value is updated. You can add this code in ngOnInit() as per your need. I don’t recommend setTimeout Solution. Let me know if this one works for anyone. 👍
Thanks
for (const key of Object.keys(this.addAgent.controls)) { if (this.addAgent.controls[key].invalid) { this.addAgent.controls[‘email’].setErrors({‘incorrect’: true}); const invalidControl = this.el.nativeElement.querySelector(‘[formcontrolname="’ + key + ‘"]’); invalidControl.focus(); invalidControl.scrollIntoView({block: “end”, behavior: “smooth”}) break; }