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

<%= 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

1

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.

  1. If you're not using LiveView, then save that template as leex instead, and it'll work as it.

  2. If you are using LiveView then there's a new way of building these forms with a form/1 function, documented here:

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like