How to use AshPhoenix.Form with the default core_components file that comes with Phoenix 1.7?
Terryble:
Phoenix 1.7 has updated the core_components file that comes with
mix phx.new
which means we need to change the way we use
AshPhoenix.Form
to play nicely with the updated form components.
From what I gathered in some of the previous conversations, the way to do it in the new component should be as follows.
When calling
<.simple_form />
, the
:let
should removed so it would look like this:
<.simple_form
for={@form}
id="post-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save">
When it comes to using
<.input />
, the
field
attribute should look like this:
<.input field={@form[:title]} type="text" label="Title" />
When defining the form, we can still use the regular
AshPhoenix.Form.for_action
,
AshPhoenix.Form.for_create
, and
AshPhoenix.Form.for_update
and then calling
to_form()
to make it compliant with the new components.
When calling the validate
handle_event
, there are no issues with just assigning
AshPhoenix.Form.validate() |> to_form()
for the form assigns in the socket.
My problem is with calling
AshPhoenix.Form.submit
. I am getting an error when doing the following:
AshPhoenix.Form.submit(socket.assigns.form, params: post_params)
I am getting the following error:
** (Protocol.UndefinedError) protocol Phoenix.HTML.FormData not implemented for {:ok, #MyApp.Blog.Post<..>} of type Tuple. This protocol is implemented for the following type(s): AshPhoenix.FilterForm, AshPhoenix.FilterForm.Arguments, AshPhoenix.FilterForm.Predicate, AshPhoenix.Form, Atom, Ecto.Changeset, Map, Plug.Conn
What should I do to fix this? For some reason, it actually persists in the database. It’s just that I’m getting this error if I call the line above.
I am using
ash_phoenix
1.2.8
Terryble:
I’m just guessing here. I’m looking at the source for
AshPhoenix.Form.submit
and it looks like
submit/2
is returning
{:ok, Ash.Resource.record()}
which breaks the call to
Phoenix.HTML.FormData.to_form/1
.
I’m not 100% and it’s just based on how I understood the source code for this.
https://github.com/ash-project/ash_phoenix/blob/v1.2.8/lib/ash_phoenix/form/form.ex#L1434
Terryble:
I forked
ash_phoenix
and I did some testing. My guess was correct -
submit/2
was indeed returning
{:ok, Ash.Resource.record()}
so the call to
Phoenix.HTML.FormData.to_form/1
will not work.
I’m not sure if I just did it wrong or if this is an actual bug.
kernel:
pretty sure you should keep the :let={f} and use that
kernel:
no need to call to_form manually
kernel:

Terryble:
That worked! I realized I was wrong because I was trying to follow the way the new generators in 1. 7 did it. Apparently, all I had to do was update
ash_phoenix
to the latest version. and that was it.
Thanks!