petal_components icon indicating copy to clipboard operation
petal_components copied to clipboard

Checkbox groups send an empty string

Open RobinBoers opened this issue 7 months ago โ€ข 2 comments

The checkbox-group component has an hidden input with an empty value:

https://github.com/petalframework/petal_components/blame/23ec5d208826311645808c42913fd2dc7e9bee41/lib/petal_components/field.ex#L240

This produces values like this in LiveView (notice the empty string):

dbg(params)
=> %{"my-group" => ["", "things", "i", "checked"]}

Which is kinda crashing our logic. We didn't have this issue before the upgrade to v3. What is the rationale behind the hidden input?

RobinBoers avatar Apr 30 '25 12:04 RobinBoers

The hidden input in checkbox groups is a standard HTML pattern to ensure the form parameter is always sent even when no checkboxes are selected. However, the empty string can be removed from the implementation. To fix this, modify lib/petal_components/field.ex line 240 to either: 1. Remove the empty value: elixir <input type="hidden" name={@name} value="" /> to elixir <input type="hidden" name={@name} /> 2. Or filter out empty strings in your form handling: elixir params["my-group"] |> Enum.reject(&(&1 == "")) The hidden input ensures the parameter exists in the form data even when no boxes are checked, but the empty value is not strictly necessary.

About Greptile

This response provides a starting point for your research, not a precise solution.

Help us improve! Please leave a ๐Ÿ‘ if this is helpful and ๐Ÿ‘Ž if it is irrelevant.

Edit Issue Bot Settings ยท Greptile

greptile-apps[bot] avatar Apr 30 '25 12:04 greptile-apps[bot]

The hidden input in checkbox groups is a standard HTML pattern to ensure the form parameter is always sent even when no checkboxes are selected.

https://github.com/petalframework/petal_components/commit/8715c035db8dd7fdaf0f80fdd6ef9f0d1bbae7e7

So then this commit is wrong? Because now the empty value is being set as the default first element in the list, while it was meant as a fallback when no checkboxes are checked?

RobinBoers avatar Apr 30 '25 13:04 RobinBoers