Fl4m3Ph03n1x
How to mark a checkbox 'checked' with liveview?
Background
I am using Phoenix’s templating language with ~H and I want to mark some checkboxes as checked depending on some condition.
To achieve this, I have this code:
defp syndicate_checkbox(assigns) do
assigns = Enum.into(assigns, %{})
if Map.get(assigns, :checked) do
~H"""
<div class="row single-syndicate">
<input class="column single-checkbox" type="checkbox" id={@synd.id}
name="syndicates[]" value={@synd.id} checked>
<label for={@synd.id} class="column"><%= @synd.name %></label>
</div>
"""
else
~H"""
<div class="row single-syndicate">
<input class="column single-checkbox" type="checkbox" id={@synd.id}
name="syndicates[]" value={@synd.id}>
<label for={@synd.id} class="column"><%= @synd.name %></label>
</div>
"""
end
end
Now, this function works but all the duplication leaves a terrible taste in my mouth.
Specifically because the only difference here is this 1 line:
<input class="column single-checkbox" type="checkbox" id={@synd.id}
name="syndicates[]" value={@synd.id} checked>
Namelly, the checked word.
Problem
Normally I would do something like:
<input class="column single-checkbox" type="checkbox" id={@synd.id}
name="syndicates[]" value={@synd.id} {mark_checked(true)}>
and somewhere in the same file:
defp mark_checked(true), do: "checked"
defp mark_checked(_), do: ""
However the issue with this is that "checked" or "" are strings, which means the code inside ~H will not compile.
Question
How do I remove this duplication?
Marked As Solved
LostKobrakai
<input … checked={boolean_value}> just works.
More generally I’d suggest using changesets for forms and let the helpers deal with the details.
1
Popular in Questions
In Ruby, I can go:
User.find_by(email: "foobar@email.com").update(email: "hello@email.com")
How can I do something similar in Elixir?
...
New
can someone please explain to me how Enum.reduce works with maps
New
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
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
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
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
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
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
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Other popular topics
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
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
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
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









