Issue with adding title and subtitle to a column in Sanity.io

30 replies
Last updated: Dec 6, 2022
Any idea why this isnt working? I can add a image, but not title or subtitle
export default {
  title: 'Columns',
  name: 'columns',
  type: 'array',
  of: [
    {
      name: 'columnTitle',
      type: 'string',
    },
    {
      name: 'columnSubTitle',
      type: 'subTitle',
    },
     {
      name: 'columnImage',
      type: 'image',
    },
  ],
};
AI Update

I can see the issue! When you define an array in Sanity, the of property should contain object types, not just plain field definitions. You're defining fields like you would in a document or object type, but array members need to be wrapped in an object type structure.

Here's the corrected version:

export default {
  title: 'Columns',
  name: 'columns',
  type: 'array',
  of: [
    {
      type: 'object',
      fields: [
        {
          name: 'columnTitle',
          type: 'string',
          title: 'Title'
        },
        {
          name: 'columnSubTitle',
          type: 'string', // Changed from 'subTitle' unless you have a custom type
          title: 'Subtitle'
        },
        {
          name: 'columnImage',
          type: 'image',
          title: 'Image'
        },
      ],
    },
  ],
};

The key changes:

  1. Wrapped everything in an object type: The of array should contain object definitions that have a type: 'object' and a fields array
  2. Added the fields property: Your title, subtitle, and image fields go inside the fields array of the object
  3. Fixed the subtitle type: Changed type: 'subTitle' to type: 'string' (unless you have a custom subTitle type defined elsewhere)

The way you had it, Sanity was treating each item as a separate type that could be added to the array (like how block content arrays can have different types of content). But what you want is a single object type that contains all three fields together.

Now when you add an item to the array, you'll get a single object with all three fields (title, subtitle, and image) that you can fill in. The image worked before because image is a valid standalone type, but string fields need to be part of an object structure within arrays.

Hey Hii
user U
Hey!
I think you should try doing string instead of "SubTitle"
And tell me if it works or not
even tho i have a object called
subTitle
?
Ya, Object name doesn't matter
This didnt work
Data type is too important
export default {
  title: 'Columns',
  name: 'columns',
  type: 'array',
  of: [
    {
      title: 'Title',
      name: 'columnTitle',
      type: 'string',
    },
    {
      title: 'Sub Title',
      name: 'columnSubTitle',
      type: 'string',
    },
    {
      name: 'columnImage',
      type: 'image',
    },
  ],
};
could be a typo somewhere?
Is there any specific reason for using array instead of Document?
Creating a “Page builder”
not really for the entire page, but a section where the user can customize themselves
Try doing document like this
The purpose of a page builder is a little different of what you sent!
Look i just want you to try
I tried it, it worked the way
document
should work
So field type as a document is working, right?
Yes
Okk
So did you tried doing some research for array in sanity docs?
Found the issue. No worries!
Can you pls tell me what was the problem?
Much appreciated

export default {
  title: 'Columns',
  name: 'columns',
  type: 'array',
  of: [
    {
      title: 'Title',
      type: 'object',
      name: 'columnTitle',
      fields: [
        {
          title: 'Title',
          name: 'title',
          type: 'string',
        },
      ],
    },
    {
      title: 'Sub title',
      type: 'object',
      name: 'columnSubtitle',
      fields: [
        {
          title: 'Sub Title',
          name: 'subtitle',
          type: 'string',
        },
      ],
    },
    {
      name: 'columnImage',
      type: 'image',
    },
  ],
};
had to wrap each string fields inside an object
Ohh i see
Thanks
user U
Any time! Thank you for spending time to help!
It was my pleasure

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?