Form_for not working in Phoenix 1.7

Hi there,

I have problem following this tutorial in Phoenix 1.7 (Create an index page):

mix phx.server
Compiling 1 file (.ex)
warning: undefined function submit/1 (expected VamosWeb.EventsHTML to define such a function or for it to be imported, but none are available)
  lib/todo_web/controllers/items_html/index.html.heex:15

warning: undefined function date_input/2 (expected VamosWeb.EventsHTML to define such a function or for it to be imported, but none are available)
  lib/todo_web/controllers/items_html/index.html.heex:12

warning: undefined function text_input/2 (expected VamosWeb.EventsHTML to define such a function or for it to be imported, but none are available)
  lib/todo_web/controllers/items_html/index.html.heex:9

warning: undefined function text_input/2 (expected VamosWeb.EventsHTML to define such a function or for it to be imported, but none are available)
  lib/todo_web/controllers/items_html/index.html.heex:6


== Compilation error in file lib/todo_web/controllers/items_html.ex ==
** (CompileError) lib/todo_web/controllers/items_html/index.html.heex:3: undefined function form_for/3 (expected VamosWeb.EventsHTML to define such a function or for it to be imported, but none are available)

My form:

<h1>Events</h1>

<%= form_for @changeset, Routes.events_path(@conn, :create), fn f -> %>

  <label>
    Add a new todo item: <%= text_input f, :title %>
  </label>
  <label>
    Add a new todo item: <%= text_input f, :description %>
  </label>
  <label>
    Add a new todo item: <%= date_input f, :date %>
  </label>

  <%= submit "Submit" %>
<% end %>

<%= for item<- @items do %>
  <h3><%= item.description %></h3>

  <%= if event.completed do %>
    <p style="color:green;">Completed</p>
  <% else %>
    <p style="color:red;">To be done </p>
  <% end %>

  <hr>
<% end %>

My items_html.ex:

defmodule TodoWeb.ItemsHTML do
  use TodoWeb, :html

  embed_templates "items_html/*"
end

Any clue what I’m doing wrong?

Phoenix v1.7 no longer imports Phoenix.HTML.Form but you can bring it back by adding import Phoenix.HTML.Form in your def html and/or def component.

1 Like

Also judging by the date of the blog post, it is more likely to be Phoenix 1.6 :slight_smile:

1 Like

Oh, ok, but why is it like that?
Why do we have to import it now and it’s not working out-of-the-box like in 1.6?
What was behind that decision?

Thanks Jose!

@kokolegorille yes, it’s 1.6, but I wanted to adapt it to 1.7 :slight_smile:

My guess is to encourage using the tidier <.form> function component instead.

2 Likes

With HEEx, it is more natural to build the components yourself with HTML, rather than hiding everything behind functions.

See how CoreComponents handles forms. :slight_smile:

3 Likes