Smart Action form field autofill from collection

Expected behavior

When creating a new action form with some fields that require be filled in, is it currently possible to show any sort of suggested values read from a collection? For example, if the form has a field “product_id”, I would like to see the suggestions for all the possible products and select one of them to submit the form action.

Context

  • Package Version: 5.1.0
  • Rails Version: 6.0.3.2
  • Database Dialect: Postgresql

Hi @sergior,

I think you should be able to achieve this using the fields options of the smart actions definition, described here. You should then be able to display a list of product to choose from by specifying the field you want with this syntax.

Hope that helps !

Hi @jeffladiray, thanks for the prompt reply.

I had a look at handling-input-values and I’ve tried to define it as product.id as suggested by the documentation. Still when I look at the interface I see a gray field (not editable) with the following message underneath it: “The search on the referenced collection is not authorized.”

Also, I don’t want all the products, I need to scope them with something like Product.available. Is this possible?

After a bit of debugging I was able to determine why it was not allowing me to edit it: I had to make sure that the Product settings in ForestAdmin had the Reference field configured. I’m now able to see a text field that I can search for a specific Product.

I still have two issues with it though:

  • The first one was the one I mentioned above, which is that I don’t want all the Products, but just a subset of it based on a filter/scope
  • I’ve tried to configure the default values for the form action following these docs but with the reference field I’m not able to pre-filled with a default value.

Hi @sergior,

Could I take a look at your smart action definition?

That might be of some help to understand your struggle and try to find a workaround.

# collection

module ForestLiana
  module Collections
    class Item
      include ForestLiana::Collection

      collection :Item
 
      action(
        'Clone item',
        type: 'single',
        endpoint: '/forest/items/clone',
        fields: [
          {
            field: 'product_id',
            type: 'String',
            reference: 'Product.available.id', # <<< the `available scope seems to be ignore all together
            description: 
            is_required: true
          }
        ]
      )
    end
  end
end
   # controller

    class ItemsController < ForestLiana::ApplicationController
      def clone_values
        context = get_smart_action_context
        item = Item.find(context[:id])

        preset_values = {
          product_id: item.product.id,
        }
        render serializer: nil, json: preset_values, status: :ok
      end
    end
 # routes

 forest_items_clone_values POST /forest/items/clone/values(.:format)
 forest_items_clone POST /forest/items/clone(.:format)
  • If it helps, I’ve managed to pre-fill the values when I’m not using the reference.
  • The Product.available.id seems to ignore the available scope and just fetch all the Products when I’m typing in the field. Is there any way I can use this scope to limit the results I get?

As for your first issue, ForestAdmin does not support default values for reference fields in their smart actions forms. If that’s really something you need, I can make an entry in our board for future priorization.

Same goes for filtered references. The reference field only state the target collection’s primary key, no scopes are taken into account in the process. I can also make an entry for that.

However, if you really need to scope this reference field select, I may have two workarounds (both having drawbacks):

  • The first one consist of making the comparison after the validation on the forest_items_clone function. That way you could send back a message stating that this entry isn’t a valid one. But as stated the drawback here is that you check it after pressing the Clone item button, which isn’t a great flow.
  • The second one would be to define your own smart collection which would be a scoped copy of the targeted table (by filtering directly in the get). That way, making the reference points to this newly created smart collection, you would ensure a scoped list.

However, let’s be transparent, this last workaround will not be easy to implement and probably hard to maintain too.

Anyway, I hope my answer will help you!

1 Like

I confirm :pensive:

About the products subset, you might be able to override the products retrieval controller (that, by default, searches in all products) to search for available products only.
You can use Referer request header to distinguish a search from the main products list and a search from your Smart Action form.

More info about routes override available here:
https://docs.forestadmin.com/documentation/v/inapp/reference-guide/routes

Hey @sergior,

I didn’t look into details, but aren’t you looking for Smart action hooks ?