Terbium-135

Terbium-135

Phoenix form: How to add 'selected' to an input field of type select

Probably I’m overlooking something very obvious…

How do I add selected to the option field depending on the value of type on rendering the form?

Was looking at the core_components.ex file for some further information but no luck

<.input field={{f, :type}} type="select">
    <:option value="meat"><%= gettext("Meat") %></:option>
    <:option value="vegan"><%= gettext("Vegan") %></:option>
    <:option value="vegetarian"><%= gettext("Vegetarian") %></:option>
</.input>

So lets say with type=“vegan” the HTML supposed to be:

    <:option value="vegan" selected><%= gettext("Vegan") %></:option>

Marked As Solved

Terbium-135

Terbium-135

Thanks for the head ups.

After replacing the old core_components.ex file with the latest one and after changing the form syntax as well all is working as expected!

<.input
  field={f[:type]}
  type="select"
  options={[
      {gettext("Salad"), "salad"},
      {gettext("Dessert"), "dessert"},
      [...]          
      {gettext("Vegan"), "vegan"},
      {gettext("Vegetarian"), "vegetarian"},
      {gettext("Info"), "info"},
  ]}
  >
</.input>

Also Liked

milangupta

milangupta

This may help you understand the behavior and some of the tricks .. am using the new syntax and latest phoenix version ..

      <.input field={f[:name]} type="text" label="Name" />
      <.input field={f[:name]} type="select" value={"Vegetarian"}
          options={[{"meat", "Meat"}, {"vegan", "Vegan"}, {"vegetarian", "Vegetarian"}]}>
      </.input>
cmo

cmo

You can put selected on the option but you shouldn’t need to.

If you “prefill” your form with a value in type you’ll see that value will be selected.

Also, that’s old syntax for field, what version are you using?

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New

Other popular topics Top

New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement