-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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
FormGroup.patchValue should ignore null arrays #21021
Comments
Hi, I've got the same here. Have you a workaround for waiting a patch ? |
I check the data for known arrays on the response. If null, set it to []. No real work-around. |
is there any update on this? |
I'm waiting for update too |
The same issue happens when patching a formGroup with a null value |
While this is not solved, I am setting it as follows:
|
This issue was solved? |
This is such an obvious thing to fix. It's really annoying :( |
Is this on roadmap or what alternative route should we use? |
A "dirty fix" in case you really need this: ⚠ This may break some stuff, use at own risk!import { NgModule } from '@angular/core'
import { FormArray, FormGroup, FormsModule } from '@angular/forms'
FormGroup.prototype.patchValue = function (value: { [key: string]: any }, options: { onlySelf?: boolean, emitEvent?: boolean } = {}): void {
let keys: string[]
try {
keys = Object.keys(value)
} catch (_e) {
keys = []
}
keys.forEach(name => {
if (this.controls[name]) {
this.controls[name].patchValue(value[name], { onlySelf: true, emitEvent: options.emitEvent })
}
})
this.updateValueAndValidity(options)
}
FormArray.prototype.patchValue = function (value: any[], options: { onlySelf?: boolean, emitEvent?: boolean } = {}): void {
value?.forEach?.((newValue: any, index: number) => {
if (typeof index === 'number' && this.at(index)) {
this.at(index).patchValue(newValue, { onlySelf: true, emitEvent: options.emitEvent })
}
})
this.updateValueAndValidity(options)
}
@NgModule({
imports: [
FormsModule
],
exports: [
FormsModule
]
})
export class FormsModuleFix { } |
Do we have this on the roadmap yet? |
Here is a replacement to "patchValue" which call "reset" on groups and array when a null value is provided safePatchValue(form: FormGroup | FormArray, value: any, options: any = {}) {
Object.keys(value).forEach(name => {
if (form.controls[name]) {
if ((form.controls[name] instanceof FormGroup || form.controls[name] instanceof FormArray)) {
if (isNil(value[name])) {
form.controls[name].reset();
} else {
this.safePatchValue(form.controls[name], value[name], { onlySelf: true, emitEvent: options.emitEvent });
}
} else {
form.controls[name].patchValue(value[name], { onlySelf: true, emitEvent: options.emitEvent });
}
}
});
form.updateValueAndValidity(options);
} |
…classes to skip `null` values Prior to this commit, the `patchValue` of the `FormGroup` and `FormArray` classes used to throw an exception when the `value` argument contained a data structure that has `null` or `undefined` as a value for a field that represents an instance of `FormGroup` or `FormArray` (for `FormControl` it's not a problem, since it doesn't have nested controls), since the `patchValue` method tried to iterate over provided value to match current data structure. This commit updates the `patchValue` logic in `FormGroup` and `FormArray` classes to just ignore `null` and `undefined` values (without any changes to corresponding `FormGroup` and `FormArray` instances). This behavior looks oinline with the `patchValue` method goal of "doing its best to match the values to the correct controls" (quote from docs). Fixes angular#36672. Fixes angular#21021.
…classes to skip `null` values Prior to this commit, the `patchValue` of the `FormGroup` and `FormArray` classes used to throw an exception when the `value` argument contained a data structure that has `null` or `undefined` as a value for a field that represents an instance of `FormGroup` or `FormArray` (for `FormControl` it's not a problem, since it doesn't have nested controls), since the `patchValue` method tried to iterate over provided value to match current data structure. This commit updates the `patchValue` logic in `FormGroup` and `FormArray` classes to just ignore `null` and `undefined` values (without any changes to corresponding `FormGroup` and `FormArray` instances). This behavior looks inline with the `patchValue` method goal of "doing its best to match the values to the correct controls" (quote from docs). Fixes angular#36672. Fixes angular#21021.
…` classes to skip `null` values Prior to this commit, the `patchValue()` of the `FormGroup` and `FormArray` classes used to throw an exception when the `value` argument contained a data structure that has `null` or `undefined` as a value for a field that represents an instance of `FormGroup` or `FormArray` (for `FormControl` it's not a problem, since it doesn't have nested controls), since the `patchValue()` method tried to iterate over provided values to match current data structure. This commit updates the `patchValue()` logic in `FormGroup` and `FormArray` classes to just ignore `null` and `undefined` values (without any changes to corresponding `FormGroup` and `FormArray` instances). This behavior looks inline with the `patchValue()` method goal of "doing its best to match the values to the correct controls" (quote from docs). Fixes angular#36672. Fixes angular#21021.
…` classes to skip `null` values (#40534) Prior to this commit, the `patchValue()` of the `FormGroup` and `FormArray` classes used to throw an exception when the `value` argument contained a data structure that has `null` or `undefined` as a value for a field that represents an instance of `FormGroup` or `FormArray` (for `FormControl` it's not a problem, since it doesn't have nested controls), since the `patchValue()` method tried to iterate over provided values to match current data structure. This commit updates the `patchValue()` logic in `FormGroup` and `FormArray` classes to just ignore `null` and `undefined` values (without any changes to corresponding `FormGroup` and `FormArray` instances). This behavior looks inline with the `patchValue()` method goal of "doing its best to match the values to the correct controls" (quote from docs). Fixes #36672. Fixes #21021. PR Close #40534
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. |
I'm submitting a...
Current behavior
Calling
formGroup.patchValue(someObject)
when someObject has a null array value, patchValue is still trying to iterate the null value.Expected behavior
Should check for
null
before trying to run the iterative process.The current implementation that allows null values in the array (ie; [null, 1234, null, 4433]) is not similar to this issue where the array value itself is null.
Minimal reproduction of the problem with instructions
check the constructor of add.component.ts
https://stackblitz.com/edit/angular-hewynt
What is the motivation / use case for changing the behavior?
So the back-end response isn't forced to send an empty array.
Environment
The text was updated successfully, but these errors were encountered: