Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

How to set a boolean field to true by default in Sanity Studio.

6 replies
Last updated: Aug 19, 2021
😟How do I make a boolean true on my sanity studio? For example, when a new document is created I want the boolean to be checked true. I used
initialValue: true
but when I created a new document it wasn't checked true by default.
Aug 18, 2021, 6:31 PM
Hi User. To confirm, is
initialValue: true
set at the top level of your boolean field? For example:

export default {
  name: 'booleanTest',
  title: 'Boolean Test',
  type: 'document',
  fields: [
    {
      name: 'title',
      title: 'Title',
      type: 'string',
    },
    {
      name: 'someBoolean',
      title: 'Some Boolean',
      type: 'boolean',
      initialValue: true,
    },
    // ...
  ],
}
As you pointed out,
initialValue
will only trigger upon the creation of a new document (i.e., it won’t retroactively set anything).
Aug 19, 2021, 6:49 AM
Ahh, I see here that everything looks good. What do you see when you run
sanity versions
in your terminal? Any chance you’re on a version earlier than 2.10.0 (when
initialValue
support at the field-level was added)?
Aug 19, 2021, 6:54 AM
Ahh, I see here that everything looks good. What do you see when you run
sanity versions
in your terminal? Any chance you’re on a version of 2.10.0 (when
initialValue
support at the field-level was added)?
Aug 19, 2021, 6:54 AM
user A
Thanks for the reply, please I created a comment system and by default when someone leaves a comment I want it to be approved that is why I used the
initialValue: true
. Later when I see any spam then I uncheck it to false on my studio.Below is how the code looks like

export default {
  name: "comment",
  type: "document",
  title: "Comment",
  fields: [
    {
      name: "name",
      type: "string",
    },
    {
      title: "Approved",
      name: "approved",
      type: "boolean",
      initialValue: true,
    },
    {
      name: "email",
      type: "string",
    },
    {
      name: "comment",
      type: "text",
    },
    {
      name: "blog",
      type: "reference",
      to: [{ type: "blog" }],
    },
  ],
  preview: {
    select: {
      name: "name",
      comment: "comment",
      blog: "blog.title",
    },
    prepare({ name, comment, blog }) {
      return {
        title: `${name} commented on ${blog}`,
        subtitle: comment,
      };
    },
  },
};
Aug 19, 2021, 7:03 AM
user A
I just upgraded my sanity yesterday
Aug 19, 2021, 7:06 AM
If your documents aren't being created by a user in the studio,
initialValue
won't work; you'll need to replicate the functionality. See the first Gotcha here .
Aug 19, 2021, 7:12 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?