initialValue for boolean field not working in Sanity Studio

5 replies
Last updated: Aug 18, 2021
đŸ˜„Hi, I added an initialValue of a boolean type to true but on the studio, it is not selected to true.
AI Update

I understand the frustration! This is actually expected behavior in Sanity Studio. The initialValue property only applies when a document is first created - it doesn't retroactively apply to existing documents or even to fields in documents that are already open.

Here are the key things to understand:

How initialValue works:

Field-level initialValue in your schema only sets the value when:

  1. A brand new document is created
  2. The field hasn't been touched yet

If you're testing on an existing document or a document that's already been created, you won't see the initial value applied.

To set a boolean field to true by default:

defineField({
  name: 'approved',
  type: 'boolean',
  initialValue: true
})

This should work, but as documented, you need to:

  1. Create a completely new document - not edit an existing one
  2. Make sure you haven't already interacted with that field (even unchecking a checkbox counts as setting a value)

Common gotchas:

  • If you opened a draft before adding the initialValue, that draft won't have it
  • If you're testing by refreshing the page on an existing document, it won't apply
  • The initialValue is set on the client side when the document form initializes, so it won't show up in existing documents in your dataset

To test it properly:

  1. Make sure your schema changes are saved and Studio has reloaded
  2. Create a brand new document (click "Create" → select your document type)
  3. The boolean should be checked immediately when the form loads

If you need more complex initialization logic or want to set values based on context (like the current user or parent document), you can use Initial Value Templates in your sanity.config.ts file instead.

Hope this clears things up! Let me know if you're still seeing issues after creating a fresh document.

Show original thread
5 replies
initialValue
will only be applied to newly created document, is the document you’re editing a new one?
No
I just created a new comment by myself and it wasn't checked. By default, it's supposed to be approved then if I see any spam then I can disapprove it.
To add a bit more context to
user G
’s comment:
Initial Value Templates allow you to define templates for the values of a new document’s fields. These values are only applied when you create a new document in the Studio – the values are not retroactively applied to existing documents or when documents are created from the client libraries.
user Q
, to confirm, you’re not getting the
approved
boolean set to
true
when you create a new document?
user M
Yes, by default I want it to be approved whenever someone creates a new comment.

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.

Was this answer helpful?