question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The `setErrors` method does not set errors

See original GitHub issue

When 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:open
  • Created 3 years ago
  • Reactions:3
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
durveshparmarcommented, Jul 22, 2022

@all

I was also facing the same issue but I think it should be resolved as per following:


const formLoginControl = this.formGroup.controls.login;
formLoginControl.valueChanges.subscribe(value=> {
       //Condition based on value
       formLoginControl.setErrors({ incorrect: true });
})

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

0reactions
abhiabhisheik646commented, Nov 7, 2022

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; }

Read more comments on GitHub >

github_iconTop 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 >
Angular Formcontrols Seterror - StackBlitz
Error handling from backend.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found