Setting form field value from controller

Hi, I would like to know how to populate a form field by passing values from the controller.

So far I’m doing this in the controller:

def new(conn, _params) do                                                                        
    render(conn, "index.html", myvalue: "hi there!")                                               
end

In my template:

<%= form_for @conn, Routes.test_path(@conn, :new), [], fn f -> %>                                  
  <%= text_input f, :myvalue, value: assigns[:myvalue] %>                                              
  <%= submit "Search" %>                                                                           
<% end %>

But I get this:

<input id="myvalue" name="myvalue" type="text">

Instead of

<input id="myvalue" name="myvalue" type="text">hi there!</input>

Have a look here: Phoenix.HTML.Form - With limited data as well as the following section - Without form data

There you should find the answer you seek.

Hey @leonardorame you want to use value: @myvalue to make sure that it’s actually referring to an assign you have set. The second thing of course is that the expected output would be:

<input id="myvalue" name="myvalue" type="text" value="hi there!" />

Note how the value is contained in the tag body, not between an opening and closing tag.

Hi @benwilson512, if I change my code to:

<%= text_input f, :myvalue, value: @myvalue %>

I get this error:

key :myvalue not found in: %{conn: %Plug.Conn{adapter: {Plug.Cowboy.Conn, :...}, assigns: %{layout: {Prueba2Web.LayoutView, "app.html"}}, body_params: %{}, cookies: %{"_prueba2_key" => 

Yes, I’ve been on that document, but the example shows a hardcoded value, instead of one passed from the controller.

That is a very strange error. Are you showing us your actual code and the error or are you editing it before you show it?

See:

Controller:

  def new(conn, _params) do                                                                        
    val = "hi there!"                                                                              
    render(conn, "index.html", myvalue: val)                                                       
  end

Template:

<%= form_for @conn, Routes.test_path(@conn, :new), [], fn f -> %>                                  
  <%= text_input f, :myvalue, value: assigns[:myvalue] %>                                          
  <%= submit "Search" %>                                                                           
<% end

This generates the following html (no error, but also no value):

<input id="myvalue" name="myvalue" type="text">

What I’m saying is @myvalue should not produce that error if the asssign is working correctly.

If you do: myvalue: <%= assigns[:myvalue] %> you’ll probably see that it is nil, something about it isn’t getting set.

Yes, the problem must be on the controller side.

I found the solution!

The problem was in the controller. I had:

    get "/test", TestController, :index                                                            
    get "/test", TestController, :new

I now replaced the 2nd “/test” to “/test2” and everything worked as expected.

Ah, yeah it was never actually going to def new it was always going to def index.