How AI is powering better personalization in e-commerce [with Vercel]. Sign up now

Auto-generate field value from another field with button in Sanity schema

5 repliesLast updated: Nov 29, 2025

Hi there, got another Sanity question. I am trying to have a field within a schema that is being auto-generated --perhaps when a button next to it is being pressed-- based on another's field value. Quite similar to how slug is being generated with the out-of-the-box slugify functionality, when the 'Generate' button is pressed. I am a bit lost and don't know where to even start looking. Can anyone point me towards the right direction? Thank you.
AI Update

Great question! You're looking to create custom field behavior similar to the slug field's "Generate" button. This requires creating a custom input component for your field.

The slug field you mentioned is actually a built-in custom component, and you can create similar functionality for any field type. Here's how to approach this:

Basic Approach

You'll need to create a custom input component that:

Key Hooks and Methods

Here are the essential pieces you'll need:

Example Implementation

Here's a basic pattern for your custom component:

import { set, unset, useFormValue } from 'sanity'
import { Button, Stack } from '@sanity/ui'

export function MyCustomInput(props) {
  const { onChange, value } = props
  
  // Read value from another field (e.g., 'title')
  const sourceValue = useFormValue(['title'])
  
  const handleGenerate = () => {
    if (!sourceValue) {
      onChange(unset())
      return
    }
    
    // Your custom transformation logic
    const generated = transformValue(sourceValue)
    
    // Update the field value
    onChange(set(generated))
  }
  
  return (
    <Stack space={2}>
      {props.renderDefault(props)}
      <Button onClick={handleGenerate} text="Generate" tone="primary" />
    </Stack>
  )
}

// Example transformation function
function transformValue(value) {
  // Your custom logic here
  return value.toLowerCase().replace(/\s+/g, '-')
}

Then in your schema, apply this component:

defineField({
  name: 'myField',
  type: 'string',
  components: {
    input: MyCustomInput
  }
})

Accessing Nested or Multiple Fields

If you need to read from nested fields or combine multiple fields:

// Single nested field
const nestedValue = useFormValue(['parent', 'child', 'fieldName'])

// Multiple fields - you can call useFormValue multiple times
const title = useFormValue(['title'])
const category = useFormValue(['category'])

const handleGenerate = () => {
  const generated = `${category}-${title}`.toLowerCase()
  onChange(set(generated))
}

Important Notes

Where to Learn More

Check out these helpful guides for more examples:

The key is understanding that useFormValue gives you access to other field values, and onChange with set() lets you update your field programmatically - combine those with a button click handler and you've got your generate functionality!

Show original thread
5 replies

Was this answer helpful?

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.

Related contributions