Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using form_for without a model or changeset

I'm trying to create a form that just has a text field and a hidden field that I'm going to use when passing data to an HTTP API, so I don't have a model associated with it, and by extension, no changeset.

Is it possible to do this and use form_for or is the answer to just write HTML to create the form?

like image 421
Spike Grobstein Avatar asked Jan 14 '16 16:01

Spike Grobstein


1 Answers

You can use form_for/4 passing a Plug.Conn as the first argument.

From the docs (specifically the "With connection data" section):

form_for/4 expects as first argument any data structure that implements the Phoenix.HTML.FormData protocol. By default, Phoenix implements this protocol for Plug.Conn, allowing us to create forms based only on connection information.

This is useful when you are creating forms that are not backed by any kind of model data, like a search form.

<%= form_for @conn, search_path(@conn, :new), [as: :search], fn f -> %>
  <%= text_input f, :for %>
  <%= submit "Search" %>
<% end %>
like image 57
Gazler Avatar answered Sep 28 '22 10:09

Gazler