Go Behind the Experience to see how Tecovas brings the West to life with Sanity 🤠 July 18th

Discussion about setting a readOnly or hidden field using document actions in Sanity.io

12 replies
Last updated: Mar 7, 2021
Is there a way to automatically set a readOnly or hidden field? I was hoping that I could create a slug field that would be generated from a number field using the
options but it doesn't get created automatically set to a value upon publish.
Mar 6, 2021, 5:01 PM
Could you build a custom input field which renders a hidden field and use a document action to set the field on publish? https://www.sanity.io/docs/document-actions#362c883e4421
Mar 6, 2021, 7:50 PM
Thank you for the link,
user L
. It seems that the document action strategy will work for what I need. I don't know about the custom input field rendering a hidden field, but what I am hoping to accomplish with the document action is to create and set a slug field upon publish using required fields in the document as input variables.
Mar 6, 2021, 7:54 PM
Also, I seem to be having difficulty locating the part of the documentation that directs you where to place your actions. I'm not sure what folder I would put
into or load it from (

I think I'm figuring it out.
Mar 6, 2021, 7:57 PM
I did not figure out how to incorporate the example action in that link, unfortunately. Anyone who can help point me in the right direction?
Mar 6, 2021, 10:53 PM
Possibly you didn’t make the sanity.json changes? Here’s a gist that given a schema with a sourceField and slug field, both set a as string will populate the slug field on publish https://gist.github.com/raffij/4a42175eb8c5f146b1d8a806787d9511
Mar 7, 2021, 8:40 AM
Thank you,
user L
, I did add the sanity.json code that was in the example but it crashed the editor in studio. I will try out your gist and see if I can get something working from there. I really appreciate your help!
Mar 7, 2021, 3:57 PM
user L
I added the code but It's still crashing the studio when I try to edit/create a new document.
Mar 7, 2021, 5:01 PM
TypeError: hook is not a function
    at HookStateContainer (/static/js/app.bundle.js:282315:19)
    at renderWithHooks (/static/js/vendor.bundle.js:17524:18)
    at mountIndeterminateComponent (/static/js/vendor.bundle.js:20203:13)
    at beginWork (/static/js/vendor.bundle.js:21317:16)
    at HTMLUnknownElement.callCallback (/static/js/vendor.bundle.js:2909:14)
    at Object.invokeGuardedCallbackDev (/static/js/vendor.bundle.js:2958:16)
    at invokeGuardedCallback (/static/js/vendor.bundle.js:3013:31)
    at beginWork$1 (/static/js/vendor.bundle.js:25924:7)
    at performUnitOfWork (/static/js/vendor.bundle.js:24875:12)
    at workLoopSync (/static/js/vendor.bundle.js:24851:22)
Mar 7, 2021, 5:02 PM
Sorry can’t offer any assitance.
Mar 7, 2021, 6:27 PM

user L
I found the issue in the code you provided and was able to see it working in action. There was a missing
module.exports =
in the SetSlugAndPublishAction.js file.
I honestly don't see a difference between what I've done and the code you provided, outside of the file structure. I was putting my files into the path
instead of
like your code. Does file structure make a difference in sanity?
Mar 7, 2021, 7:35 PM
It's because I copied it from 1 file into many and didn't think about that.
Mar 7, 2021, 7:43 PM
No worries! I'm grateful for your help!
Your first response is what is leading me to my solution, the code example you provided helped me resolve my issues and get me moving. It turns out the only difference between the code I got from that documentation link and the code you provided was the folder structure? Weird!
Mar 7, 2021, 7:44 PM

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?