chenbobby

chenbobby

"form_for" vs. ".form" vs "f = form_for"

In various code samples, I see different ways to create a form in a .heex template.

What is the difference between using the following:

<%= form_for @changeset, "/to_path", ...other_opts, fn f -> %>
...
<% end %>
<%= f = form_for @changeset, "to_path, ...other_opts %>
...
<% end %>
<.form let={f} for={@changeset} action={"to_path"} ...other_opts>
...
</.form>

Is there a best practice, or is it just up to the preferences of the developer?

Marked As Solved

DecoyLexi

DecoyLexi

You’re right about the difference. According to the LiveView docs, <.form> is just a wrapper around form_for and should generally be preferred if you’re using HEEx templates (including on deadviews). Just remember to give the action attribute!

See Phoenix.LiveView.Helpers documentation under “Outside LiveView”

Also Liked

arrowsmith

arrowsmith

I know this thread is old, but I was struggling with the same question, so I’ll post the answer in case anyone else comes across this thread like I did.

tl;dr:

  • form_for should no longer be used and will eventually be removed from Phoenix.
  • In LiveView, use <.form for={@form}> where @form is a Phoenix.HTML.Form struct (generally created using to_form). Don’t use :let.
  • Outside of LiveView, you can also pass a changeset or map to for, and it’ll be converted to a Form which can be captured with :let. (This approach is also technically possible in a LiveView, but it’s discouraged because it makes change tracking less efficient.)

The second and third points are made clear in the docs for form/1. The first point was confirmed to me by none other than Chris McCord; I wrote about it here:

josevalim

josevalim

Creator of Elixir

Correct. .form is the way to go, form_for will forever exist for backwards compatibility, f = form_for is deprecated and should be rewritten.

chenbobby

chenbobby

I found some answers in the Phoenix.HTML docs:

  1. <%= f = form_for @changeset ... %> is now deprecated. Use <%= form_for @changeset ... %> instead.

  2. <.form> is a “function component”, in the context of Phoenix LiveViews.

This leads me to a follow-up question: If I don’t use LiveViews, should I still use <.form>?

Where Next?

Popular in Questions Top

myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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

We're in Beta

About us Mission Statement