Show notice message for field independent of validation rules

13 replies
Last updated: Jun 23, 2023
Hello there,Please is it possible on sanity to set a notice message for a field that is showed everytime independently of rule validation ?
AI Update

Yes, absolutely! You can add a persistent notice message to a field using the description property in your schema definition. This description will always be visible below the field title, regardless of validation state.

Here's how to do it:

defineField({
  name: 'title',
  type: 'string',
  title: 'Page Title',
  description: 'This title will appear in search engine results and browser tabs',
})

The description appears as persistent help text below the field label and stays visible at all times - it's not dependent on validation rules or user interaction.

Enhanced formatting with JSX

If you need more complex formatting or styling in your notice message, you can use JSX in the description:

defineField({
  name: 'url',
  type: 'url',
  title: 'Canonical URL',
  description: (
    <>
      <strong>Important:</strong> This URL will be used by search engines to index this content.
      Make sure it matches your production URL.
    </>
  ),
})

Combining with validation messages

The description field is completely independent of validation rules. So you can have:

  • A persistent description that's always visible
  • Validation errors/warnings that appear conditionally when rules aren't met
defineField({
  name: 'email',
  type: 'string',
  title: 'Contact Email',
  description: 'This email will be publicly visible on your contact page',
  validation: (Rule) => Rule.required().email(),
})

In this example, the description is always shown, while validation messages only appear when the field is empty or contains an invalid email.

This approach is covered in the Sanity schema documentation on field definitions, where descriptions are recommended as a best practice for providing context to content editors about how fields will be used.

Show original thread
13 replies
Thank you, I did it and it is showing
Now is it possible for a datetime to show notice message with a specific colour please ?Like this,
I found a way to use custom.css but when I try I'm stuck here:
That looks like a validation message not a description…
But I think if you want to do that you’ll have to create a custom component
https://www.sanity.io/docs/component-api
Or do it with validation
https://www.sanity.io/docs/validation which is probably the better choice here.
You could have a description that says “Enter the match time in your local time.” And then add a warning/error if it’s not what you expect? Because making a description red like that signals (to me at least) that there’s an error but the date input is empty so why would it be red?
I tought the same thing but the product owner wants it like that
I personally would push back but that’s me. So you’ll have to go about creating a custom component and maintaining that. Because className doesn’t look to be a valid option for datetime https://www.sanity.io/docs/datetime-type
Ok thanks
The
description
field can take a React component, so you could try setting it to something like:

description: (
  <>
    <div style={{ color: 'red' }}>This is important text.</div>
  </>
),
// rest of schema...
Tried but it is not working
Working well, Thanks. was using ts file instead of tsx
Given those errors, you’re probably in a .ts file. It will need to be tsx.
Thank you very much

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?