Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Select options without GraphQL name validation

default discord avatar
p.tek2 years ago
4

Hi,



I have a select field where I want the user to be able to select a time of day, in the format of '00:00', '00:30', etc. As the Payload documentation for the Select field specifies, this fails validation for GraphQL enumeration values.



Is there a workaround for this? Some option to make the select field less strict? I can think of several cases where you might want a "regular" HTML select field, without the Name validation.



I did also experiment with the Date field, but it transfers/saves the entire date-time, and that is not ideal for my use case.



I can use a text field with input validation instead, but that is arguably more work for the user.

  • discord user avatar
    jarrod_not_jared
    2 years ago

    You could probably set the labels to your "00:00" format and adjust the values of the options to something usable by graphql enums



    [{ label: '00:00', value: '00_00' }]
  • default discord avatar
    p.tek2 years ago

    Thank you for the suggestion! The value has to start with an underscore or a letter as well as using no special characters, e.g. 'T00_00', and the client(s) would have to reformat this to the real value before use (or use a lookup table).



    So far, I have reverted to using a text field with a format validation. Hoping Payload will support "lenient select" before I get around to implementing a custom field / component combo 🙂

  • discord user avatar
    jarrod_not_jared
    2 years ago

    I don’t think we will because we use enums for the select field in GraphQL so we are bound to the restrictions that come with it



    You could wire up an after read hook so the value gets altered before getting to the client though

  • default discord avatar
    p.tek2 years ago
    That worked well. I'll use the after read hook as you suggested. Thank you!

    Edit: Unfortunately, I spoke too soon. It worked when testing on a draft document for a versioned collection, but as soon as I tried to publish, a frontend validation error was triggered in the admin UI.



    I'll stick with the text field for now. Thank you anyway.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.