pre-fill Create Form in Admin Panel

default discord avatar
sandrowegmann
9 months ago
8

Is there any simple way to pre-fill the create form in the admin panel when creating a new doc? I've seen you can somehow solve it with a custom component, but I'm wondering if there is no better way, like a custom hook for the admin panel (similar to beforeDuplicate)



I was quite surprised I couldn't find anything about it, since it seems to me that this is quite a common thing to do?

  • default discord avatar
    paulpopus
    9 months ago

    Does setting a default value fulfill your needs?

    https://payloadcms.com/docs/fields/overview#default-values
  • default discord avatar
    sandrowegmann
    9 months ago

    Unfortunately not. I have a collection "registrations" and a collection "companies". When a registration arrives, a company should either be linked or created. It can't be fully automated because registrations rely on user Input, so the way I'd solve it is to provide a relationship field in the registration to either link or create a company.



    For that to work though, the company info needs to be pre-filled with the data from the registration fields.

  • default discord avatar
    paulpopus
    9 months ago

    Hmmm...I dont have full oversight on your data structure but

    maybe

    you could put something together using a mix of

    validate

    functions on fields and

    condition

    functions to hide other fields until the relationship one is fulfilled



    However, for the smoothest UX, custom components would help a lot



    that way you can also streamline the process

  • default discord avatar
    sandrowegmann
    9 months ago

    hmm, such a pity. It's not a huge deal to write a custom component, but I just hate overcomplicating things, since the default functionality would be perfect (apart from the pre fill part)

  • discord user avatar
    jarrod_not_jared
    Payload Team
    9 months ago

    I would love to weigh in here but am having trouble following. Any way your could throw together a repo and link it here? Even if it’s simplified from your use case

  • default discord avatar
    sandrowegmann
    8 months ago

    Hey @jarrod_not_jared , sorry for the late reply. I could create a demo repo if really necessary, but the functionality I'm looking for is pretty simple: Prefill certain fields in the Create Form.



    Example: I have a collection of Companies, and a collection of Contacts. Every contact belongs to a company and has a relationship field that links to it. Now, I want to add a "create Contact" Button to the Company Edit Page. This is straight forward using a Custom Field component, however, the relationship field in the new contact should be automatically set to the comopany that I'm coming from

  • discord user avatar
    jesschow
    Payload Team
    7 months ago

    Hey @sandrowegmann how did you get on with this?

  • default discord avatar
    sandrowegmann
    7 months ago

    Got it solved! Was planning to create a video explaining how I achieved it



    For the record, Issue solved, I've created a video explaining how I did it:



    https://www.youtube.com/watch?v=TzeKIbAZ80Q
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.