Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Field in Phoenix Templates

I know Phoenix provides some nice Railsy helpers that can be used in forms like:

  • text_input
  • number_input
  • date_select
  • and submit

but I can't find one for select fields. I've been searching the Phoenix Docs, but couldn't find anything.

So my question is, is there a Phoenix helper for select fields in forms?

like image 367
Sheharyar Avatar asked Nov 29 '22 07:11

Sheharyar


1 Answers

I should've been searching the Phoenix.HTML Docs (Thanks to José for pointing this out!)

The helper for select is:

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>
like image 193
Sheharyar Avatar answered Dec 19 '22 07:12

Sheharyar