Ash FrameworkAF
Ash Frameworkโ€ข3y agoโ€ข
8 replies
Jmanda

** (KeyError) key :options not found: Error on AshPhoenix.Form submit

This is my form
<.simple_form
    for={@form}
    id="todo-form"
    phx-target={@myself}
    phx-change="validate"
    phx-submit="save"
  >
    <div class="row">
      <div class="col col-sm-12 col-md-6">
        <.input field={@form[:title]} type="text" label="Title" />
      </div>
    </div>

    <:actions>
      <.button phx-disable-with="Saving...">
        <i class="fa-solid fa-floppy-disk mr-2"></i>Save Todo
      </.button>
    </:actions>
</.simple_form>

In my FormComponent:

impl true
  def update(%{todo: todo} = assigns, socket) do
    form =
      if todo do
        AshPhoenix.Form.for_action(todo, :update,
          as: "todo",
          api: MyApp.Todos
        )
      else
        AshPhoenix.Form.for_action(MyApp.Todos.Todo, :create,
          as: "todo",
          api: MyApp.Todos
        )
      end

    {:ok,
     socket
     |> assign(assigns)
     |> assign(:form, form |> to_form())}
  end

The form is rendering and validations are working fine, however on submiting the form am getting KeyError:
[error] GenServer #PID<0.1208.0> terminating
** (KeyError) key :options not found in: #AshPhoenix.Form<resource: MyApp.Todos.Todo, action: :create, type: :create, params: %{"title" => "asdf"}, source: #Ash.Changeset<action_type: :create, action: :create ...
...


If I change the form to use :let:
<.simple_form
  :let={f}
    for={@form}
    id="todo-form"
    phx-target={@myself}
    phx-change="validate"
    phx-submit="save"
....

and change the input field:
<div class="col col-sm-12 col-md-6">
  <.input field={{f, :title}} type="text" label="Name" />
</div>

I get the following error:
key :title not found in: %{__changed__: nil, __given__: %{__changed__: nil, field: {%Phoenix.HTML.Form{source: #AshPhoenix.Form<resource: MyApp.Todos.Todo, action: :create ........

What could be the issue? Is there something am missing?
Was this page helpful?