<p>This isn't really an answer. But I think I do know what might be happening. The default browser behavior is that checkbox values only get submitted when it has the attribute 'checked' (why? I have no idea). In php you're essentially testing whether the post value exists or not. If it exists, true; if not, false. I assume for the built-in checkbox fieldtype that craft does this for you in the background (by somehow comparing with the original options). You'll have to figure out a way to perform this check yourself. Maybe take a look at the source files and see how craft is handling this.</p>

CC BY-SA 3.0
2015-01-20T18:32:27.493Z
If I recall, we add a hidden input for each checkbox that has an actual 0 or 1 value.
Brad Bell
CC BY-SA 3.0
2015-01-20T23:23:51.577Z
Thanks, I figured that could be an approach, a hidden field, but wasn't sure if there was a better route. I was able to resolve the issue.
mz91184
CC BY-SA 3.0
2015-01-20T18:17:38.513Z
2015-01-20T18:17:38.513Z
Douglas McDonald
<p>If it helps anyone else, if you have a checkbox field in Craft with multiple options, you can use the hidden field approach with a value of <code>[]</code>.</p>

<p>For example, two checkboxes, one with <code>value="newsletter"</code> and another with <code>value="events"</code>, both with <code>name="marketing[]</code>.</p>

<p>Then include a hidden field above them with <code>value="[]"</code> and <code>name="marketing"</code>.</p>

<p>Important to leave off the <code>[]</code> from the hidden field's name.</p>

<p>This will mean that if no checkboxes are checked, it will set all of them to null/unchecked in Craft. However, as soon as any are checked, they will overwrite the hidden field safely.</p>

CC BY-SA 4.0
2019-12-03T17:09:26.630Z
2019-12-03T17:09:26.630Z
iamkeir
<p>I created a custom field type to show user groups in a checkbox layout, e.g. (<a href="http://cl.ly/image/0A1a140m031h" rel="nofollow">http://cl.ly/image/0A1a140m031h</a>).</p>

<p>I am running into one small issue. If I add content with a checkbox(s) option selected and save the entry. Then I try to edit the entry and uncheck all the options, the entry will still have the original checkbox options selected and won't return to the null state as expected. I was trying to use prepValueFromPost but that doesn't seem to run unless the value is set. How do I fix this?</p>

<custom-fieldtype>
Custom Fieldtype with Checkboxes - Remove value and set to null when checkboxes are left blank
# Custom Fieldtype with Checkboxes - Remove value and set to null when checkboxes are left blank

(Asked by: username_0 on 2015-01-20)

I created a custom field type to show user groups in a checkbox layout, e.g. (<http://cl.ly/image/0A1a140m031h>).

I am running into one small issue. If I add content with a checkbox(s) option selected and save the entry. Then I try to edit the entry and uncheck all the options, the entry will still have the original checkbox options selected and won't return to the null state as expected. I was trying to use prepValueFromPost but that doesn't seem to run unless the value is set. How do I fix this?

**Tags:** custom-fieldtype


---

## 2 answers

### Answer by username_1 on 2015-01-20. Score: 1 | Accepted answer

This isn't really an answer. But I think I do know what might be happening. The default browser behavior is that checkbox values only get submitted when it has the attribute 'checked' (why? I have no idea). In php you're essentially testing whether the post value exists or not. If it exists, true; if not, false. I assume for the built-in checkbox fieldtype that craft does this for you in the background (by somehow comparing with the original options). You'll have to figure out a way to perform this check yourself. Maybe take a look at the source files and see how craft is handling this.

> **Comments:**
> **username_2**: If I recall, we add a hidden input for each checkbox that has an actual 0 or 1 value.  
> **username_0**: Thanks, I figured that could be an approach, a hidden field, but wasn't sure if there was a better route. I was able to resolve the issue.  

---

### Answer by username_3 on 2019-12-03. Score: 1

If it helps anyone else, if you have a checkbox field in Craft with multiple options, you can use the hidden field approach with a value of `[]`.

For example, two checkboxes, one with `value="newsletter"` and another with `value="events"`, both with `name="marketing[]`.

Then include a hidden field above them with `value="[]"` and `name="marketing"`.

Important to leave off the `[]` from the hidden field's name.

This will mean that if no checkboxes are checked, it will set all of them to null/unchecked in Craft. However, as soon as any are checked, they will overwrite the hidden field safely.


---

