Auto-populate the field values based on the values in other fields

System Information
  • Strapi Version: v4
  • Operating System: macOS
  • Database: MySQL
  • Node Version: 16.15
  • NPM Version: 5.8.8
  • Yarn Version: 1.22.18

I am trying to populate a field based on the value in other fields.
Example: I have a content type with fields city = Mumbai and country = India, now I need a third field called market which should have the value Mumbai - India (i.e. value of city field + β€œ-” + value of country field).

Can someone please help me with this?


Can only point you in a vague direction, as I haven’t played with this myself, but I would create a custom controller for the content type and extend the update function, which fires every time you save your content type (or so I believe).

In this function, I’d check whether city and country are set and, if so, set the market like you said in your post.

I’d also do the same for the create function.

You can read more about controllers here: Backend customization - Controllers - Strapi Developer Docs

1 Like

Finally with the help of internet forums and awesome people out there, I was able to solve my issue using lifecycle webhooks.

Here is the stack overflow link to an awesome answer to this problem in case you want to just see the code.