marick
Phoenix.HTML.Form has a checkbox function, but it seems targeted at standalone boolean checkboxes, like hiding/showing a password:
However, another common case (I think) is to select from a list of options, like this:
(Because I don’t think many ordinary humans are comfortable using multiple-select <select> tags.)
When I created this form, I wanted the POST to produce a list of chosen_animal_ids. My code was this:
checkbox(f, :animal_ids,
name: "#{input_name(:animals, :chosen_animal_ids)}[]",
value: a.id,
hidden_input: false)
That doesn’t actually work. Unlike other Phoenix.HTML.Form functions, value: here doesn’t set the HTML value attribute. The result of the above is this HTML:
<input type="checkbox" value="true">
(I don’t understand the checkbox documentation for value: “the value used to check if a checkbox is checked or unchecked. The default value is extracted from the form data if available”. So I could be missing something obvious.)
Working code is this:
checkbox(f, :animal_ids,
name: "#{input_name(:animals, :chosen_animal_ids)}[]",
checked_value: a.id,
# ^^^^^^^
hidden_input: false)
Questions:
- Am I doing something completely wrong?
- Are there libraries that extend Phoenix.HTML.Form that I should know about? (I’ve only found this which addresses a different concern.)
- Would it be useful for Phoenix.HTML.Form to have a special function for this case (like the one I’m about to write) - or documentation that describes the issue? (In addition to the
valueconclusion, I think the explicitname: "#{input_name(:animals, :chosen_animal_ids)}[]"is kind of gross.)
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming













Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kokolegorille
I have build my helper for this case, it is mostly coming from this post
It is checked, not checked_value
Here is one I made for Bootstrap 4
and I call it like this
Some of the private functions are missing, but there are not difficult to reproduce.
There is nothing specific to Phoenix, I do remember having the same problem with Rails
marick
Yes, it’s “value” if you use
tag, “checked_value” if you usecheckbox.kokolegorille
ah, ok
marick
Actually, working with
checkboxis awkward enough that I think you were right to work withtag.