So, I want LiveView to keep track of a set of checkboxes and know which ones are checked and which are not. The purpose of this is to keep track of, and modify, a many_to_many association between billing_categories and credit_cards.
In my .html.heex I have:
<div>
<%= category %>:
<input checked={category in @changeset.data.billing_categories} type="checkbox" phx-click="flip" value={category}>
</div>
<% end %>
And then in my live controller:
def handle_event("flip", params, socket) do
IO.inspect(params, label: "Well looky here!")
{:noreply, socket}
end
When I check the box with the name "1", I get this in the terminal from inspecting the params:
Well looky here!: %{"value" => "whoa!"}
But when I uncheck a box, I just get:
Well looky here!: %{}
If I could just get handle_event/3 to know which box has been unchecked, I could take care of the rest. Is there some simpler way to do this?
The behaviour you see is how html is supposed to work. Unchecked checkboxes are ignored when gathering a forms data.
I‘d suggest using the form helpers of phoenix to build checkboxes. They automatically add an hidden input as well (before the checkbox), which uses the same name and has an „unchecked“ value. So when the checkbox is not checked you get the value of the hidden input – when the checkbox is checked you get the value of the checkbox, which overrides the hidden input value.