Replies: 1 comment 1 reply
-
That's nearly correct. After calling def list_entities(params) do
with {:ok, %Flop{filters: filters} = flop} <- Flop.validate(params) do
filters = Flop.Filter.put_new(filters, %Flop.Filter{field: :some_field, op: :empty, value: true})
{:ok, Flop.run(Entity, %{flop | filters: filters}, for: Entity)}
end
end The fields={[some_field: [label: "some field", op: :empty, type: "checkbox", default: true]]}
That doesn't look too hacky to me. But be aware that value={if(i.rest[:invert_value], do: !Phoenix.HTML.Form.normalize_value("checkbox", i.value), else: i.value)} The |
Beta Was this translation helpful? Give feedback.
-
Thanks for the suggestions in this issue: #297 (comment)! I hope it's ok I take the discussion here, to keep the issue clean.
Defining the checkboxes like that works, although it took a while to get it all right. The main problem I had to solve had to do with the default setting of the filter. The hidden input takes care of the case when you've submitted the form with the checkbox unchecked, but it doesn't help with the initial render/mount, when no data has yet been submitted. I used
Flop.Filter.put_new/2
to add a default filter, when no filter was defined in the params. Manipulating the params directly felt very wrong, because it's an indexed map (%{"0" => ...}
) where the order could be easily changed. This appears in a context module somewhere:I'm not sure if this can be simplified somehow. It's a bit unfortunate that the params are validated twice, like this. Maybe I'm missing something here, but this is as clean as I could get it.
The other issue had to do with how the actual value was bound to the checkbox input. Since it's somewhat inverted (when it's checked the value is
""
, while when it's unchecked the value should betrue
), I also had to jump through a hoop to forward the value attribute to the checkbox input:I've added the
unchecked_value
andchecked_value
attributes for the checkbox input, which works great.Just wanted to share the issues I had. Might be interesting to think about how this can be improved, or to notice something missing in the library itself regarding setting defaults like I did (although I don't have any suggestions right now).
Beta Was this translation helpful? Give feedback.
All reactions