SelectInput custom field

default discord avatar
s4chin11 months ago
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
    mweggersen11 months 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
    s4chin11 months ago

    I will try this

  • discord user avatar
    jarrod_not_jared
    11 months 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
    s4chin11 months 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
    11 months 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
    s4chin11 months 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
    11 months ago

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

  • default discord avatar
    s4chin11 months 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
    11 months ago

    oic, you will likely want to use a

    json

    field type instead of text field type for your custom field

  • default discord avatar
    s4chin11 months 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
    11 months ago

    yeah no problem!

  • default discord avatar
    banks08 months ago

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

  • default discord avatar
    s4chin8 months ago

    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
    banks08 months ago

    Can you link the whole component file?

  • discord user avatar
    jarrod_not_jared
    8 months ago

    @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 help straight from the Payload team with an Enterprise License.