blisscs
Phoenix.Html.Form Question. How to use "array names" for example name[] for input name
Hello,
I got a little question here.
In Html we sometimes we append ‘’ to the input names in form, as an example below.
<input name="input1[]" type="text" />
<input name="input1[]" type="text" />
<input name="input1[]" type="text" />
...
My questions are -
First - In General, how do phoenix handle these kind of inputs? Do phoenix converts to them tuple type on the fly as they are in ordered. Let’s try it out.
Second - How do use Phoenix.Html.Form to generate the tags in the form. I have some think that it should work but it didn’t.
This is what I have tried -
<%= form_for @conn, @action, [as: "purchase"], fn f -> %>
<%= text_input f, :roll %>
<%end %>
The above code resulting in generating below tag -
<input name="purchase[roll[]]" type="text" />
Which isn’t what we want?
Dev.
Marked As Solved
josevalim
Pass the name option to the functions, for example: text_input f, :input1, name: "input1[]". Not sure if that’s exactly the option but it something along the lines above.
Also Liked
axelson
For anyone that arrives here via google and is looking to see how to add an array field within a model you can use
<%= text_input f, :input1, name: "game[player_names][]" %>
So that will add a player_name array within the game_params in your controller.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance










