Is there an Array string in Payload CMS? #1791
-
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
Hi @IRediTOTO, this isn't currently supported but would definitely be useful in many cases. This question was also discussed on discord here. A potential workaround solution would be to:
There might be more appropriate workarounds too if you want to share more information on what you're looking to achieve, let us know if you need further help here! |
Beta Was this translation helpful? Give feedback.
-
The |
Beta Was this translation helpful? Give feedback.
-
Hey @IRediTOTO I am also a payload newbie and I have came across the same issue and after so many tries and seeing this discussion I have found this way to be able to store the data. I know its already been answered but I still want to share this one hope this will be helpful or correct me if I am following the wrong approach.
import { CollectionConfig } from "payload/types";
const Users: CollectionConfigs = {
slug: "users",
fields: [
{
name: "restrictions",
label: "Restrictions",
type: "array",
fields: [
{
name: "preferences",
type: "text",
required: true,
},
],
hooks: {
afterRead: [
({ value, originalDoc }) => {
value = originalDoc?.preferences?.map((value: string) => {
return { preferences: value };
});
return value;
},
],
},
},
{
name: "preferences",
type: "text",
hasMany: true,
admin: {
readOnly: true,
},
hooks: {
beforeChange: [
({ value, siblingData}) => {
const preferences =
siblingData.restrictions.map((el: any) => el.preferences) || [];
delete siblingData["restrictions"];
value = preferences;
return value;
},
],
},
},
]
} |
Beta Was this translation helpful? Give feedback.
-
In 2025, there is now the 'hasMany' option for a.o. the text, select and number field, which should do exactly what you want 😃 See e.g. https://payloadcms.com/docs/fields/text @IRediTOTO |
Beta Was this translation helpful? Give feedback.
The
json
field might work for you: https://payloadcms.com/docs/fields/json#json-field