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.

SelectInput custom field

default discord avatar
s4chinlast year
15

According to this video

https://www.youtube.com/watch?v=Efn9OxSjA6Y

, I managed to create a custom field and got required data using useFormFields as recommended by @Mitth but I m having an issue when I use hasMany prop. The value is not saved after hitting save .

  • default discord avatar
    mweggersenlast year

    I think you are missing a getter and setter for the new field. See here for docs on it.


    https://payloadcms.com/docs/admin/preferences

    Spesificly this line:


    const { getPreference, setPreference } = usePreferences();



    where the getter and setter are defined. i havent done an implamantaion with this but it should be fairly straight forward if you look at the example.

  • default discord avatar
    s4chinlast year

    I will try this

  • You can see from the source code that the onChange is a bit differrent when you have multiple options, check this out:

    https://github.com/payloadcms/payload/blob/master/src/admin/components/forms/field-types/Select/index.tsx#L64-L85
  • default discord avatar
    s4chinlast year

    oh yes



    On change did solve my issue on setting the value but I m getting this type issue which is of course I have custom field type set to text which I cant change to array

    image.png
  • Do you need it to be stored in the DB?



    I feel like a

    ui

    field would be perfect for this

  • default discord avatar
    s4chinlast year

    Yes its a cart thats why and admin can add into some add ons in users cart, product is generally free but add ons are not and add ons varies according to product thats why I have to keep it there

  • Well I understand that for addOns, but what is the need for selectedAddOns? Or am I misunderstanding?

  • default discord avatar
    s4chinlast year

    User can only select add ons from a single product he cant add another product or add ons from another product

  • oic, you will likely want to use a

    json

    field type instead of text field type for your custom field

  • default discord avatar
    s4chinlast year

    Thank you for your help. I m very new to this and still exploring. Changing type to json helped.

  • yeah no problem!

  • default discord avatar
    banks0last year

    Could you link the code? Im having the same issue...

  • default discord avatar
    s4chinlast year

    change type to json and in your custom select field instead of Select input just use Select from "payload/components/forms"

    image.png
    image.png
  • default discord avatar
    banks0last year

    Can you link the whole component file?

  • @banks this is just like the date field in the other thread you have going

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..