Reputation: 1
I am new to Phoenix/Elixir and am trying to make a little chat app. I'm having some difficulties and hope you can help.
Here is my code
<%= f = form_for :chat, "#", id: "chat-form", phx_submit: :submit_message %>
<%= text_input f, :message, placeholder: "Enter Message" %>
</form>
This throws the following error
(Phoenix.LiveView.HTMLTokenizer.ParseError) lib/chat_web/live/room_live.html.heex:13:5: unmatched closing tag. Expected </div> for <div> at line 6, got: </form>
Removing the closing tag presents me with the following error
(CompileError) lib/chat_web/live/room_live.html.heex:11: undefined function form_for/3
Please can someone help me find out why it isn't working
Upvotes: 0
Views: 2335
Reputation: 61
If you are using a recent version of phoenix_live_view, .heex
templates are used now and it should be written like this (also don't forget to import Phoenix.HTML.Form
):
<%= form_for @chat_changeset, "#",
[id: "chat-form",
phx_submit: :submit_message], fn f -> %>
<%= text_input f, :message, placeholder: "Enter Message" %>
<% end %>
Upvotes: 0
Reputation: 98
Take a look at the new documentation here: https://hexdocs.pm/phoenix_html/Phoenix.HTML.Form.html#form_for/3
The new syntax looks like this:
<%= form_for :chat, "#", id: "chat-form", phx_submit: :submit_message, fn f -> %>
<%= text_input f, :message, placeholder: "Enter Message" %>
<% end %>
Upvotes: 1
Reputation: 600
Looks like you're falling afoul (like I just did) of the fact that building forms as you are, is what's suggested by the Phoenix docs, but you're using heex
templates which enforce that the tags in your HTML are correct.
So there's two solutions here, depending on what sort of app you're writing.
If you're not using LiveView, then save that template as leex
instead, and it'll work as it.
If you are using LiveView then there's a new way of building these forms with a form/1
function, documented here: https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.Helpers.html#form/1
Upvotes: 0
Reputation: 120990
According to the documentation for Phoenix.HTML.Form.form_for/3
, the first argument is expected to be of Phoenix.HTML.FormData.t()
type, while you are passing an atom there.
Somewhat alongside the below lines would work.
<%= f = form_for @changeset, "#", id: "chat-form", phx_submit: :submit_message %>
<%= text_input f, :message, placeholder: "Enter Message" %>
</form>
Upvotes: 2