egor

egor

Union Forms and NoSuchInput Error

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.

Marked As Solved

egor

egor

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.

Where Next?

Popular in Questions Top

New
Tee
can someone please explain to me how Enum.reduce works with maps
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement