Select options without GraphQL name validation

default discord avatar
p.tek
3 months ago
6

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.

  • default discord avatar
    jarrod69420
    3 months 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.tek
    3 months 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 🙂

  • default discord avatar
    jarrod69420
    3 months 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.tek
    3 months 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.

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.