How to add a reference in an enum field in SmartAction Forms?

I am trying to add a dropdown to the smart action form that shows a list of names of any model, in this example rooms. In the handler method I then want to assign the selected room to a house. I only find examples where the “Room” is mapped onto an enum field, using the rooms name. How can I forward the room ID instead to the handler method, but display the room name in the dropdown?

For example, I might have a house with 3 bedrooms, so every room name is “Bedroom”. Since “name” is not a unique identifier, it is not a reliable way to look up the model in the database.

I would like to do something like mapping the rooms to a label/value pair. The given example only shows {object Object} in the dropdown for rooms.

collection('house', {
  actions: [
    {
      name: 'Add a room',
      type: 'single',
      endpoint: '/forest/actions/add-room',
      fields: [
        {
          field: 'room',
          type: 'Enum',
          enums: [],
          isRequired: true
        },
      ],
      hooks: {
        load: async ({ fields, request }) => {
          const rooms = await models.room.findAll({ attributes: ['id', 'name'] })
          const roomField = fields.find(field => field.field === 'room')
          roomField.enums = rooms.map(r => ({
            value: r.id.toString(), 
            label: r.name 
          }))
          return fields
        }
      }
    }
})



async function addRoomHandler (req, res) {
  const { query, user } = req
  const recordsGetter = new RecordsGetter(house, user, query)
  const houseIds = await recordsGetter.getIdsFromRequest(req)
  // how to get selected room id here?
}

Hi @marksrr,

It’s pretty simple to do it on our latest agent (https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/actions/forms-static#references-to-records).
But you are clearly not on it. Which version are you using ?

Normally you can do something like on forest-express-sequelize:

        {
          field: 'room',
          type: 'String', // Or number if your id is a Number
          // Notice this line, it will tell us that this is referencing actually another collection
          // It should be `name_of_my_collection.primary_key_name`
          reference: 'room.id',
          isRequired: true
        },

Hey Vince,

thanks for your response.

unfortunately this project is running on forest-express-sequelize. Do you see any chance to implement it there?

The code I shared is for foret-express-sequelie. Sorry it was not explicit enough :sweat_smile:

:wink:

But that creates the normal searchable relation field, which I find a bit inconvenient if we only have like 5 instances for example. Changing the type to Enum does not work in that case. Can I modify the field from typeahead to dropdown in the smart action form as well somehow?

And maybe you can quickly point me to another thing: When I add a “search” key to a field to pre-filter the resources, I get “The search on the referenced collection is not authorized.”. From what I found I have to allow it somewhere in the settings, but I could not find the right place.

Your help is highly appreciated thank you

Could you try to do the following, I’m not sure it will work. It’s well supported on the new agent but I’m not sure about forest-express-sequelize.

        {
          field: 'room',
          type: 'String', 
          reference: 'room.id',
          widget: 'dropdown', // Widget here might modify what is displayed
          isRequired: true
        },
1 Like

That did the trick, thank you Vince!

You can do that by accessing your roles settings.

  • Go to your project settings by clicking on the top left corner on the name of your project.
  • Then click on Roles
  • Click on the role you want to edit
  • In the collections permissions, you need to have the Read (list) permissions checked for the collections you are searching on
1 Like