egor

egor

I’m building an application with a plugin system where each component can have different configuration parameters based on the plugin type selected.

I’ve implemented the Union Forms approach from the documentation for my plugin parameters, but I’m encountering an issue.

My Setup

I have a resource (Plugin) with a params attribute that uses a union type to accommodate different plugin parameter structures:

defmodule MyApp.Plugins.Plugin do
  use Ash.Resource, otp_app: :my_app, domain: MyApp.Plugins, data_layer: AshPostgres.DataLayer

  # Other configuration...

  attributes do
    integer_primary_key :id
    attribute :name, :string
    attribute :active, :boolean, default: false
    attribute :plugin_type, :module
    # This is the union attribute that holds different parameter configurations
    attribute :params, :plugin_params
    # Other attributes...
  end

  # Actions, relationships, etc.
end

The union type for plugin parameters:

defmodule MyApp.Types.PluginParams do
  defmodule TypeAParams do
    use Ash.Resource, data_layer: :embedded

    attributes do
      attribute :window_size, :integer, default: 3000
      attribute :threshold, :float, default: 0.70
      # Other parameters...
    end

    actions do
      defaults [:read, :destroy, create: :*, update: :*]
    end
  end

  use Ash.Type.NewType,
    subtype_of: :union,
    constraints: [
      types: [
        type_a: [
          type: TypeAParams,
          tag: :type,
          tag_value: :type_a
        ]
        # Other plugin types...
      ]
    ]
end

In my form component:

<.inputs_for :let={fc} field={@form[:params]}>
  <.input
    field={fc[:_union_type]}
    phx-change="type-changed"
    type="select"
    label="Plugin Type"
    options={["Type A": "type_a"]}
  />

  <%= case fc.params["_union_type"] do %>
    <% "type_a" -> %>
      <.input type="number" label="Window Size" field={fc[:window_size]} />
      <% # Other type_a inputs... %>
  <% end %>
</.inputs_for>

The Issue

When rendering the form, the current values are correctly displayed in the inputs. However, when I try to change any input in the nested form, I get this error:

%Ash.Error.Invalid.NoSuchInput{
  calculation: nil,
  resource: MyApp.Types.PluginParams.TypeAParams,
  action: :update,
  input: "window_size",
  inputs: MapSet.new([]),
  did_you_mean: [],
  # Other error details...
}

I’m following the type-changed handler from the documentation:

def handle_event("type-changed", %{"_target" => path} = params, socket) do
  new_type = get_in(params, path)
  path = :lists.droplast(path)

  form =
    socket.assigns.form
    |> AshPhoenix.Form.remove_form(path)
    |> AshPhoenix.Form.add_form(path, params: %{"_union_type" => new_type})

  {:noreply, assign(socket, :form, form)}
end

Any insights on what I might be doing wrong? The form renders correctly with the initial values but fails when trying to update any field.

Showing Posts 1 to 2

zachdaniel

zachdaniel

Creator of Ash

Something definitely seems strange there. The new_type definitely lines up with one of the union type names? The adding and removing paths are correct? If it ends up being a bug I’ll probably need a reproduction to pin it down as the union form rendering is a bit complex internally.

egor

egor OP

I figured out the issue. Turns out the bug wasn’t in Ash itself, but in the docs! The examples for union forms were missing public? true on the embedded resource attributes, which caused the updates to fail. I’m gonna submit a PR (#324) to fix that.

To reproduce the problem I’ve created a repo minibikini/ash_phoenix_union_form_example which now has a working example of a union form in Ash, perhaps someone will find it useful.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews