-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Reactive Form - Formgroup with a nested formgroup throw exception on reset call with null values #20509
Comments
I would like to take this issue. The error occurred on We can check Here is my idea: Could you please advise me a little here? Thanks. |
Any update on this? |
This still happens, currently caused by the angular/packages/forms/src/model.ts Lines 1677 to 1684 in bd34bc9
It blindly assumes As a workaround, we're using the following reset code. Note that this modifies the input object which might be undesired, but in our use case it doesn't matter. YMMV. export const resetForm = (group: FormGroup, value?: any, options?: {
onlySelf?: boolean;
emitEvent?: boolean;
}) => {
const ensureValues = (g: FormGroup, v: any) => Object.keys(g.controls).forEach(key => {
// The list of controls can change (for ex. controls might be removed) while the loop
// is running (as a result of invoking Forms API in `valueChanges` subscription), so we
// have to null check before invoking the callback.
const control = g.controls[key];
if (control instanceof FormGroup) {
// Ensure the value to be bound to this formgroup is not null.
v[key] = v[key] || {};
// Recursively check child controls for nested formgroups.
ensureValues(control, v[key]);
}
});
ensureValues(group, value || {});
group.reset(value, options);
}; |
Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends. Find more details about Angular's feature request process in our documentation. |
Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage. We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package. You can find more details about the feature request process in our documentation. |
We'll need to see how breaking this fix is. I really hope it's not too breaking in g3 -- that would mean people are relying on app crashes 🤦 |
Non typed forms allow to pass null to nested groups when calling `formGroup.reset()`, this commit prevent an undefined access. fixes angular#20509
Non typed forms allow to pass null to nested groups when calling `formGroup.reset()`, this commit prevent an undefined access. fixes angular#20509
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
) Non typed forms allow to pass null to nested groups when calling `formGroup.reset()`, this commit prevent an undefined access. fixes angular#20509 PR Close angular#48830
Current behavior
When i call form reset on a formGroup with nested formGroup passing it with a null value i receive a error "Cannot read property ... of null"
Expected behavior
Expected result is that on form reset with a null value the nested form group properties become all null.
Minimal reproduction of the problem with instructions
When i have a formGroup with a nested formGroup like this:
If i call a form reset or a patch value with a object like this:
i receive the error "Cannot read property 'Id' of null" because he try to read the nested property from a null object
Expected result is that on form reset with a null value the nested form group properties become all null. This behavior happen when i call the reset without the nested properties:
form.reset({Id: 3})
What is the motivation / use case for changing the behavior?
Usually service return models with all properties and nested entities null if they don't exist instead return the nested entities initialized
The text was updated successfully, but these errors were encountered: