Form_For Issue in Phoenix
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 This Throws the...
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
3 Answers
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>
Take a look at the new documentation here:
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 %>
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
leexinstead, and it'll work as it.If you are using LiveView then there's a new way of building these forms with a
form/1function, documented here: