-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(Forms) Expose FormControl errors to parent FormGroup #10530
Comments
How to distinguish group errors from control errors? For example we can have 2 controls that are both valid but produce invalid combination. I may want to distinguish such errors from control level errors. |
Other use case is that on a large form, with potentially tabs (which hides visually errors), it becomes unclear why a form is invalid and we might have to go through each tab to tackle the error. Hence having a summary of the errors at the top form level helps a lot making the form valid. |
Along with what @Krisa mentioned, having that summary of errors easily available is nice when trying to log common problems users encounter via analytics. |
@Krisa |
@manklu That would work for something very simple. But in my case, the tabs are actually "dynamic", in the sense it's placed within a FormArray of unknown length, and there can even be tabs of tabs, or FormArray of FormArray. So at the end, it's just one massive form where I need to have a "summary" of all errors. |
Got the same problem. It's extremely confusing, I understand the need of distinguishing group errors, but there could be and extra property containing all the child errors.. that would help. Same for status, if there's any child pending, the parent group should know about it. |
Well... once again creating wrapper directive... |
+1 for this feature request. |
+1 |
Has there been any consideration on picking this up? I'd expect a lot of people working with complex forms are creating workarounds for this like we had to, effectively creating methods that recursively traverse the tree of controls looking at each control's |
I came across the same problem. But it's not the first issue I have with forms... I was really excited when reactive forms were released. But the more you use it in real world applications the more issues I encountered. Is there some issue which contains all the current problems / feature requests / improvements for the forms module? What I collected the last days:
|
I stumbled upon the duplicate #11530 from Google and... Yeah, I can't believe this isn't a thing. I think that any way of implementing it would've been better than the current nothing, even if we don't have a way to track back to the original invalid control. |
It'd definitely be a nice thing to have that included in Angular itself. In the meantime, we've created at work an open source library called We do have access to all the nested errors 🎉 Example with that form, where And the errors that are accessible from the top level form component: If you're interested, you can find the live demo here: https://cloudnc.github.io/ngx-sub-form And the source code of the demo here: https://github.com/cloudnc/ngx-sub-form/tree/master/src/app
@NatoBoram hopefully that'll help you :) |
Still no one from angular team interested in help for developers? |
Recursive iterating over the FormGroup works for me: public group: FormGroup;
public getErrors(group?: FormGroup | FormArray): { [key: string]: ValidationErrors } | ValidationErrors[] | null {
const errors = group instanceof FormArray ? [] : {};
if (group == null) {
group = this.group;
}
let idx = 0;
Object.keys(group.controls).forEach((name) => {
idx++;
const control = group.get(name);
if (control instanceof FormArray || control instanceof FormGroup) {
const tmpErrors = this.getErrors(control as (FormArray | FormGroup));
if (tmpErrors != null) {
errors[name] = tmpErrors;
}
} else if (control instanceof FormControl) {
if (control.errors == null) {
return null;
}
if (group instanceof FormArray) {
(errors as ValidationErrors[])[idx] = control.errors;
} else {
errors[name] = control.errors;
}
}
});
return (group instanceof FormGroup && Object.keys(errors).length === 0)
|| Array.isArray(errors) && errors.length === 0 ? null : errors;
} |
Any chance someone from the Angular team might acknowledge this issue before it's four year anniversary? |
Lol it came and went...of all the requests people have for reactive forms, this seems like one of the more straightforward ones to solve in SOME capacity. |
I don't want to over-react, but recently I've started to vue things differently. Having started with Angular, I just assumed that all large open source projects just let heavily requested developer issues languish for years at a time. Come to find out, there are actually some large projects that actively work to resolve developer issues! They don't even have to make announcements about finally committing enough resources to get their issue tracker under control, because as crazy as this sounds, they never ignored the issues in their issue tracker in the first place!. Apparently pull requests are not supposed to take years for approval either! Who knew? |
I stumbled onto this as well, and revamped the solution to find invalid control names found on stackoverflow, to return control errors recursively: public findErrorsRecursive(
formToInvestigate: FormGroup | FormArray
): ValidationErrors[] {
let errors: ValidationErrors[] = [];
const recursiveFunc = (form: FormGroup | FormArray) => {
Object.keys(form.controls).forEach((field) => {
const control = form.get(field);
if (control?.errors) {
errors.push(control.errors);
}
if (control instanceof FormGroup) {
recursiveFunc(control);
} else if (control instanceof FormArray) {
recursiveFunc(control);
}
});
};
recursiveFunc(formToInvestigate);
return errors;
} |
I faced the same problem with form.invalid. It was false while the form group has a child form control with null value while required. I tried to use !form.valid and it worked fine. So I think it is a temporary solution till Angular Team solves the issue. |
Hi, just want to provide an update: we've discussed this feature request with the team and the conclusion is that we'd like to add the support to gather errors for the entire control tree, but doing so would increase bundle size for all apps (since it'd need to be added to the This feature request is in the Backlog now, but we don't have any ETA at this moment. |
@AndrewKushnir surely there is a way to offer some sort of helper functions to do this (if bundle size is really what is holding this up). If you look at Stackoverflow you'll see incredibly high demand for such a feature. Why not offer some well tested helper functions - I don't want to spend hours reading over all the nuances of each of these options. |
let me fix the sentence: *six years 🦁 |
@azhdarshirinzada your timeline makes me think we've got more chance of seeing you post a comment notifying everyone here next year saying "*7 years" rather than getting a contribution from you to fix this issue. Feel free to prove me wrong though. But as a reminder Angular is a free and open source project. You cannot have the same expectation as if you were paying something. Contribute yourself, pay a bounty for someone to raise a fix for you, or take a seat on the quiet bench of people kindly waiting for this to be solved. We can all see the date of the issue and seeing comments like yours isn't helping in any way. Anyone moderating this thread feel free to mark my comment as off topic (and @azhdarshirinzada 's comment too while we're at it...) |
If you use a
Despite the validation state, also the pristine and dirty state can be propagated to the parent form. A complete example of such a base class can be found here: https://gist.github.com/cbossi/0979c7dcdc2e215e28772a11ccc602ab |
I was looking at this and wondering if it's possible to make a change to
TO
Then, you would be able to call it like below to connect the touched, pristine, dirty, pending, and validity between the FormGroup created in the component, and the FormControl attached to the component via directive.
This would be instead of hooking into |
I think this would be a great application of the unified events. I have found myself wanting something like an |
I'm submitting a ... (check one with "x")
Current behavior
When there are errors in a form group, the formGroup.status is 'INVALID' but the formGroup.errors property is null.
ex.
Expected/desired behavior
When the validity of a FormControl changes, have the parent FormGroup.errors merged with the errors from the controls. This is similar to how the angularjs formController.$error property works.
https://docs.angularjs.org/api/ng/type/form.FormController
What is the motivation / use case for changing the behavior?
This would be helpful because I prefer to only disable a submit button when specific errors are on a form, such as required. With this I could drive that attribute off the formControl.errors value instead of formControl.status.
ex.
The text was updated successfully, but these errors were encountered: