ash-project/ash_phoenix

`form?: auto` not work properly with embedded field in union type

Closed this issue · 0 comments

Describe the bug

defmodule Todo do
  use Ash.Resource, data_layer: :embedded

  attributes do
    attribute :body, :string, allow_nil?: false
  end

  actions do
    defaults [:read, create: [:body], update: [:body]]
  end
end

defmodule TaskRequest do
  use Ash.Resource, data_layer: :embedded

  attributes do
    attribute :todo_list, {:array, Todo}, allow_nil?: false
  end

  actions do
    defaults [:read, create: [:todo_list], update: [:todo_list]]
  end
end

defmodule Content do
  use Ash.Type.NewType,
    subtype_of: :union,
    constraints: [
      types: [
        task_request: [
          type: TaskRequest,
          tag: :type,
          tag_value: :task_request
        ]
      ]
    ]
end

defmodule Post do
  use Ash.Resource, data_layer: Ash.DataLayer.Ets, domain: Domain

  attributes do
    uuid_primary_key :id
    attribute :title, :string, allow_nil?: false
    attribute :content, Content, allow_nil?: false
  end

  actions do
    defaults create: [:title, :content], update: [:title, :content]
  end
end

form =
  Post
  |> AshPhoenix.Form.for_create(:create, forms: [auto?: true])
  |> to_form()
  |> AshPhoenix.Form.add_form(:content,
    params: %{"type" => "task_request", "todo_list" => [%{}]}
  )
** (exit) an exception was raised:
    ** (AshPhoenix.Form.NoFormConfigured) todo_list at path [] must be configured in the form to be used with `inputs_for`. For example:

There is an attribute called `todo_list` on the resource `TaskRequest`, and it is
accepted by `TaskRequest.create`.

Perhaps it is not a type that can have forms generated for it?


Available forms:



Example Setup:

    Form.for_create(
      Resource,
      :action,
      params,
      forms: [
        # For forms over existing data
        todo_list: [
          type: :list,
          as: "form_name",
          data: <current_value>,
          resource: RelatedResource,
          update_action: :update
        ]
        # For forms over new data
        todo_list: [
          type: :list,
          as: "form_name",
          create_action: :create
        ]
        # For forms over both
        todo_list: [
          type: :list,
          as: "form_name",
          data: <current_value>,
          resource: RelatedResource,
          update_action: :update,
          create_action: :create
        ]
      ]
    )

        (ash_phoenix 2.0.0-rc.8) lib/ash_phoenix/form/form.ex:4880: Phoenix.HTML.FormData.AshPhoenix.Form.to_form/4
        (phoenix_live_view 0.20.14) lib/phoenix_component.ex:2450: Phoenix.Component."inputs_for (overridable 1)"/1
        (phoenix_live_view 0.20.14) lib/phoenix_live_view/tag_engine.ex:92: Phoenix.LiveView.TagEngine.component/3
        server_union.exs:138: anonymous fn/3 in SamplePhoenix.SampleLive.render/1
        (phoenix_live_view 0.20.14) lib/phoenix_live_view/diff.ex:391: Phoenix.LiveView.Diff.traverse/7
        (phoenix_live_view 0.20.14) lib/phoenix_live_view/diff.ex:573: anonymous fn/3 in Phoenix.LiveView.Diff.traverse_comprehension/5
        (elixir 1.16.1) lib/enum.ex:1826: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
        (elixir 1.16.1) lib/enum.ex:1826: [Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3](url)

To Reproduce

https://gist.github.com/nallwhy/5691a136817b58f0f63b5f75860d2a0f

Expected behavior

no crash

Runtime

  • Elixir version: 1.16.1
  • Erlang version: 26
  • OS: mac
  • Ash version: 3.0.0-rc.36
  • any related extension versions
  • Ash Phoenix version: 2.0.0-rc.8

Additional context
Add any other context about the problem here.