Open ysfaran opened 7 months ago
I just noticed something else: Isn't it also a bug that you can change the stored data by returning a value in the afterChange
hook?
The test i wrote actually proves that you can change the data even afterChange
, which is confusing 🤔
The docs also don't state that you should return something.
In my original case I used the afterChange
hook for a side effect to delete an item of another collection, which IMO is the use case for such a hook. If you want to change the data, which the afterChange
hook was triggered for you should probably use beforeValidate
or beforeChange
I would assume.
@JessChowdhury let's review this PR together when you have bandwidth.
Can some one clarify if it is really intended, that you can change data when a value is returned from afterChange
?
If yes, there should be some explanation in the docs.
If no, this is another bug and should be fixed.
Link to reproduction
https://github.com/ysfaran/payload/tree/reproduction-after-change-bug
Describe the Bug
When you create an
afterChange
hook for any nested field type liketype: 'group'
the hook will set the value forpreviousValue
wrongly:This happens because in the code the
previousValue
of a field is always checked against the root of a collection/global. For example if you have this kind of structure defined in your collection/global:And you change the value of
group.name
, then thepreviousValue
in theafterChange
hook would NOT besubValue
butrootValue
. This only happens because they have the same field name (here literallyname
). If there is no root field with the same namepreviousValue
will always beundefined
.I already applied a
yarn patch
for my case and could work arround this with following diff:But this would only fix it for the
group
field type, not for other nested types likeblocks
orarray
.I happy to file a PR the upcoming days, if no one takes it right away :)
To Reproduce
I will just use the example of the reproduction link here:
Menu
Global like this:name
in the admin panel and save.name
toinitialValueRoot <the text you entered>
, but also unexpectedly thesubmenu.name
field toinitialValueRoot initialValueSub
.Payload Version
2.5.0
Adapters and Plugins
No response