Determining if record exists in `condition` (condition has no access to `data.id`)

default discord avatar
ssyberg
5 months ago
42

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?

  • default discord avatar
    notchr
    5 months ago

    @ssyberg How are you writing the condition? Can you explain a little more in detail please? 🙂

  • default discord avatar
    ssyberg
    5 months ago

    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;
  • default discord avatar
    notchr
    5 months ago

    Hmm



    I think I'm confused hehe, do you mean hide the collection if there is no data?



    Or hide the field?

  • default discord avatar
    ssyberg
    5 months ago

    Hide the field

  • default discord avatar
    notchr
    5 months ago

    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.
  • default discord avatar
    ssyberg
    5 months ago

    Hmm not sure if that helps

  • default discord avatar
    notchr
    5 months ago

    Could that be a solution?



    hmm

  • default discord avatar
    ssyberg
    5 months ago

    I'm happy with the id field as is, I just want to access it in the condition callback

  • default discord avatar
    notchr
    5 months ago

    Right, but it doesn't exist until published right?

  • default discord avatar
    ssyberg
    5 months ago

    Correct! That's exactly what I want to leverage!

  • default discord avatar
    notchr
    5 months ago

    Then the condition check is accurate then



    right?

  • default discord avatar
    ssyberg
    5 months ago

    Yes, for the pre-create state it's perfect



    But after record creation, there is still no

    id

    available in the condition callback

  • default discord avatar
    notchr
    5 months ago

    Ahhhh

  • default discord avatar
    ssyberg
    5 months ago

    A silly workaround would be to populate a hidden field during a create hook, but that really seems non-ideal

  • default discord avatar
    notchr
    5 months ago

    What did data log?



    in the condition cb

  • default discord avatar
    ssyberg
    5 months ago

    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

  • default discord avatar
    notchr
    5 months ago

    How about siblingData?

  • default discord avatar
    ssyberg
    5 months ago

    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?

  • default discord avatar
    notchr
    5 months ago

    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

  • default discord avatar
    ssyberg
    5 months ago

    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
Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.