Select a field in Phoenix templates

I know I Phoenix

provide some useful Railsy helpers that can be used in forms like:

  • text_input

  • number_input

  • date_select

  • and submit

but I cannot find it for fields select

. I searched Phoenix Docs but couldn't find anything.

So my question is, is there Phoenix helper

to select fields in forms?

+4


source to share


2 answers


I had to look in the Phoenix.HTML

Docs (thanks Jose for that!)

Assistant for select

:

select (form, field, values, opts \\ [])




Examples:

# Assuming form contains a User model
select(form, :age, 0..120)
#=> <select id="user_age" name="user[age]">
#   <option value="0">0</option>
#   ...
#   <option value="120">120</option>
#   </select>

select(form, :role, ["Admin": "admin", "User": "user"])
#=> <select id="user_role" name="user[role]">
#   <option value="admin">Admin</option>
#   <option value="user">User</option>
#   </select>

select(form, :role, ["Admin": "admin", "User": "user"], prompt: "Choose your role")
#=> <select id="user_role" name="user[role]">
#   <option value="">Choose your role</option>
#   <option value="admin">Admin</option>
#   <option value="user">User</option>
#   </select>

      

+13


source


It is there, the docs are in the Phoenix.HTML project: http://hexdocs.pm/phoenix_html/



+9


source







All Articles