I'd like to conditionally show some fields only if the record has already been saved once (so I can use it's id elsewhere), it looks like
data.id
is generally not available in the condition callback. Any thoughts?
@ssyberg How are you writing the condition? Can you explain a little more in detail please? 🙂
Sure I just verified this in a fresh install, here's the collection (just a slight modification of the Example collection
import { CollectionConfig } from 'payload/types';
// Example Collection - For reference only, this must be added to payload.config.ts to be used.
const Examples: CollectionConfig = {
slug: 'examples',
admin: {
useAsTitle: 'someField',
},
fields: [
{
name: 'foo',
type: 'text',
},
{
name: 'someField',
type: 'text',
admin: {
condition: (data, _) => {
if (data && data.id) {
return true;
} else {
console.log('Hide it!');
return false;
}
},
},
},
],
};
export default Examples;
Hmm
I think I'm confused hehe, do you mean hide the collection if there is no data?
Or hide the field?
Hide the field
Ahh
Well you can override the default ID field, though I'm not sure if that will then be available?
https://payloadcms.com/docs/fields/overview#customizable-id
Collections ID fields are generated automatically by default. An explicit id field can be declared in the fields array to override this behavior. Users are then required to provide a custom ID value when creating a record through the Admin UI or API. Valid ID types are number and text.
Hmm not sure if that helps
Could that be a solution?
hmm
I'm happy with the id field as is, I just want to access it in the condition callback
Right, but it doesn't exist until published right?
Correct! That's exactly what I want to leverage!
Then the condition check is accurate then
right?
Yes, for the pre-create state it's perfect
But after record creation, there is still no
id
available in the condition callback
Ahhhh
A silly workaround would be to populate a hidden field during a create hook, but that really seems non-ideal
What did data log?
in the condition cb
data log shows all the data... except the id 😂
This is in a different context, but here's an example of the data object after create:
{
"blogs": 0,
"_status": "draft",
"programs": 0,
"projects": 0,
"hero": {},
"meta": {},
"streamContained": 0,
"supplementalLinks": 0
}
Now here's where it gets even weirder
How about siblingData?
I'm seeing multiple calls to the condition callback on render, 7 in this case. The first one has the id! But none of the subsequent ones do.
siblingData
is no different
Possible this is actually a bug?
well not sure if it's a bug, but maybe an oversight
The weird thing is that the first one has ID
condition will run once for every field IIRC
in my test case, the first two logs return the id
wait jk only the first one
yeah this might be a bug
@ssyberg I'd make an issue on GH
Ok will do!
condition will run once for every field IIRC
I wonder why I'm seeing it run so many times for a single field
https://github.com/payloadcms/payload/issues/2524
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.