SelectInput custom field

default discord avatar
s4chin
4 weeks ago
11

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
    _mitth
    4 weeks ago

    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
    s4chin
    4 weeks ago

    I will try this

  • discord user avatar
    jarrod_not_jared
    Payload Team
    4 weeks ago

    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
    s4chin
    4 weeks ago

    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
  • discord user avatar
    jarrod_not_jared
    Payload Team
    4 weeks ago

    Do you need it to be stored in the DB?



    I feel like a

    ui

    field would be perfect for this

  • default discord avatar
    s4chin
    4 weeks ago

    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

  • discord user avatar
    jarrod_not_jared
    Payload Team
    4 weeks ago

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

  • default discord avatar
    s4chin
    4 weeks ago

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

  • discord user avatar
    jarrod_not_jared
    Payload Team
    4 weeks ago

    oic, you will likely want to use a

    json

    field type instead of text field type for your custom field

  • default discord avatar
    s4chin
    4 weeks ago

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

  • discord user avatar
    jarrod_not_jared
    Payload Team
    4 weeks ago

    yeah no problem!

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.