👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Cannot Read Only Property 'ListingElements' of Object '#<Object>'

2 replies
Last updated: Jun 17, 2021
Hello everyone!Hope someone can help me with an issue. I have 2 fields in object. I’d like to choose something from the array of “reference” at the first field. And depends on it make a request and fill the next field automatically. I’m working with it in a custom validation Rule at the first field, but I have an error “`Error validating value: Cannot assign to read only property 'listingElements' of object '#&lt;Object&gt;`’”

It’s appears on line “`doc.parent.listingElements = productRefs;`”
Is it posible to do what I want? Please contact me in case you know what I’m talking about.
Jun 16, 2021, 7:18 PM
You should be able to achieve this by creating a custom Input component - one that renders the standard select menu for the references, and, after it is updated, patches the document to set the value for the second field.
You can't use the validator to set field values, but you can use
Rule.custom
to validate it based on the reference. I.e.:

validation: Rule => Rule.custom((value, context) => {
   // get the parent object
   const parent = context.parent;
   
   // If there is a reference in the first field,
   // make this one required
   if (parent.referencesField.length > 0) {
      return Rule.required()(value);
   }
   // Otherwise, return true (field is valid)
   return true;
 
})
Jun 17, 2021, 1:10 AM
Thank you!
Jun 17, 2021, 7:49 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?