payload
payload copied to clipboard
Default value does not work with select and "required" is ignored.
Describe the Bug
With a select
Field
and defaultValue
, upon creating a new document, the defaultValue
is presented in the field, as intended. However, upon submitting the form to create that document, it is ignored/left empty.
On top of that, this field has required
set to true. So, even if this isn't done at the database level with SQL adapters, I still think it should still throw an error when a field is marked as required and for whatever reason, it does not receive a value before saving it to the database. Validation does not seem to take place at the right level, in my opinion.
https://github.com/user-attachments/assets/f11e7b1e-38d3-4435-b354-241ec138e7d8
Link to the code that reproduces this issue
https://github.com/HarleySalas/payload-3-reproductions/tree/defaultvalue-required-select
Reproduction Steps
You can add a field to any payload collection like this:
{
name: 'roles',
type: 'select',
hasMany: true,
defaultValue: 'editor',
required: true,
options: [
{
label: 'Admin',
value: 'admin',
},
{
label: 'Editor',
value: 'editor',
},
],
},
or alternatively, clone the reproduction I provided and create a new user once you're logged in, from the admin panel. Up doing so, simply ignore the default value, proceed to create the new user and observe that the value is completely ignored.
Which area(s) are affected? (Select all that apply)
area: core
Environment Info
Payload: 3.2.2
Node: 22.2.0
NextJS: 15.0.3