In 'group' field, required
and defaultValue
do nothing
#1186
Replies: 2 comments 1 reply
-
Related discussion: https://payloadcms.com/community-help/discord/optional-group-field
There is no solution, the suggested hack "put a condition on the group" doesn't work. |
Beta Was this translation helpful? Give feedback.
-
As an update to this very old discussion. At some point we did add {
name: 'cta',
type: 'group',
fields: [
{
name: 'label',
type: 'text',
required: false, // this is not required so you can remove this
validate: (value, { siblingData }) => {
if ( !value && siblingData.url) return 'Please provide a label for the the CTA'
return true
},
},
{
name: 'url',
type: 'text',
required: false, // this is not required so you can remove this
validate: (value, { siblingData }) => {
if ( !value && siblingData.label) return 'Please provide a URL for the CTA'
return true
},
},
],
}, |
Beta Was this translation helpful? Give feedback.
-
As of version 1.1.4, fields of type
'group'
are always required, regardless of the value of therequired
property. I think this is a waste of expressiveness, especially since the semantics of an optional group field are straightforward: the whole field may beundefined
.For example in one of my collections I'd like to have an optional CTA with required fields. I may or may not have a CTA, but if I have one it must have both label and URL. This is how I would like to express that:
But this currently requires the whole group to be defined, so it complains about undefined
label
andurl
.The semantics of
defaultValue
are consistent with those ofrequired
, namely it's ignored. Now I can't come up with a good example, but I guess it might be useful to have different default values for single fields and for the whole group.Beta Was this translation helpful? Give feedback.
All reactions