Default value for string field with radio layout in Sanity
The default: true property you're trying to use in the list options won't work for setting a default value for string fields with radio layouts. Instead, you need to use the initialValue property at the field level (not inside the options).
Here's the correct way to set a default value for a string field with a radio layout:
{
name: 'alignment',
type: 'string',
initialValue: 'left', // Set default here
options: {
layout: 'radio',
list: [
{ title: 'Left', value: 'left' },
{ title: 'Right', value: 'right' }
],
}
}The initialValue property should be a sibling of name, type, and options - not nested inside the list items. This will pre-select the "Left" option when creating new documents.
Important note about objects: If your string field is inside an object type, initialValue works differently. According to the Sanity documentation on string types, you cannot set initialValue on individual fields within an object. Instead, you'd need to set the initial value at the parent object level or use Initial Value Templates for more complex scenarios.
For example, if this field is inside an object:
{
name: 'myObject',
type: 'object',
initialValue: {
alignment: 'left' // Set default for the object's field here
},
fields: [
{
name: 'alignment',
type: 'string',
options: {
layout: 'radio',
list: [
{ title: 'Left', value: 'left' },
{ title: 'Right', value: 'right' }
],
}
}
]
}Show original thread7 replies
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.