Skip to content
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

Closed
ShiftySituation opened this issue Dec 14, 2017 · 14 comments
Closed

FormGroup.patchValue should ignore null arrays #21021

ShiftySituation opened this issue Dec 14, 2017 · 14 comments
Labels
area: forms feature Issue that requests a new feature freq2: medium
Milestone

Comments

@ShiftySituation
Copy link

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

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


Angular CLI: 1.5.5
Node: 8.2.1
OS: win32 x64
Angular: 5.0.5

Browser:
- [x] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
@khylias
Copy link

khylias commented Dec 18, 2017

Hi, I've got the same here. Have you a workaround for waiting a patch ?

@ShiftySituation
Copy link
Author

I check the data for known arrays on the response. If null, set it to []. No real work-around.

@kara kara added freq2: medium feature Issue that requests a new feature labels Dec 19, 2017
@ngbot ngbot bot added this to the Backlog milestone Jan 23, 2018
@nirajkrz
Copy link

is there any update on this?

@Eivyses
Copy link

Eivyses commented Feb 22, 2019

I'm waiting for update too

@SchneiterT
Copy link

The same issue happens when patching a formGroup with a null value

@rodolfojnn
Copy link

rodolfojnn commented Dec 11, 2019

While this is not solved, I am setting it as follows:

this.frmMain.patchValue({
    ...data,
    someProperty: data.someProperty || {}
});

@leoderigo
Copy link

This issue was solved?

@wottpal
Copy link

wottpal commented Apr 14, 2020

This is such an obvious thing to fix. It's really annoying :(

@eduardosivd
Copy link

Is this on roadmap or what alternative route should we use?

@HitkoDev
Copy link

HitkoDev commented May 9, 2020

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

@SunilManthenaG01
Copy link

Do we have this on the roadmap yet?

@mistic100
Copy link

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

@AndrewKushnir
Copy link
Contributor

AndrewKushnir commented Jan 21, 2021

Related issue: #36672, also another potentially related issue: #12219.

AndrewKushnir added a commit to AndrewKushnir/angular that referenced this issue Jan 22, 2021
…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.
AndrewKushnir added a commit to AndrewKushnir/angular that referenced this issue Jan 22, 2021
…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.
AndrewKushnir added a commit to AndrewKushnir/angular that referenced this issue Jan 24, 2021
…` 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.
jessicajaniuk pushed a commit that referenced this issue Jan 25, 2021
…` 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
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Feb 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: forms feature Issue that requests a new feature freq2: medium
Projects
None yet