2

I have a frontend entry form with some checkboxes that is not behaving correctly.

If you uncheck all options and save, nothing changes (this is bad). However if you uncheck one item and check another item it saves correctly.

Additionally in the Craft CP if you uncheck all items it saves correctly so it clearly works there.

relevant template code:

{% for volunteer in craft.entries.section('careers').search('positionType:volunteerPosition') %}
  <label class="d-block"> 
        <input type="checkbox" class="form-check-input" name="fields[volunteerPositions][]" class="form-check-input" value="{{volunteer.id}}" {% if volunteer.id in selectedEntries %}checked{% endif %}>&nbsp;&nbsp;{{volunteer.title}}  (created by {{volunteer.author }}) 
      </label> 
{% endfor %}

Please advise.

2 Answers 2

6

Your code is correct but you need to add another line near the top:

 <input type="hidden" name="fields[volunteerPositions]" value="">

From the documentation:

If you are including a Checkboxes field on a front-end entry from, make sure to include a hidden input before the checkboxes, so that an empty value is submitted if no checkboxes are checked.

1
  • N/P: this has tripped me up before too! Commented Oct 13, 2017 at 19:16
0

I think this might be related to form-data rather than being an actual Craft-issue.

Basically - when you submit your form, the data gets passed to the server. If we have checkboxes A, B and C with names checkboxes[] - and we check them all, the server would receive a checkboxes array looking like this:

['A', 'B', 'C']

However, if you check none of them and submit, the checkboxes array would look like this:

[]

So basically - in order to "uncheck" previously checked boxes in saved data, you have to implement your own "unchecking" routine.

Mocking it up really quickly:

foreach savedData.checkboxes as i => checkbox
    savedData.checkboxes[i] = in_array(checkbox, checkboxes)
endforeach

Basically you loop through all checkboxes in the savedData, and set it to checked if it also exists in the checkboxes array; otherwise you set it to "unchecked"

3
  • @fuglesetrampen I'm not following what I should do. Is this javascript? or twig?
    – CreateSean
    Commented Oct 13, 2017 at 12:58
  • It's neither, I just mocked it up without using a language at all Basically, what you would have to do, is have a plugin receive the formData (I dunno if you have this already?) and in that plugin handle checking (pun not intended) if your checkboxes are checked or not, and set the data on the EntryModel (I'm assuming) that you're about to save. Commented Oct 13, 2017 at 13:32
  • Or - I suppose you could handle it through JavaScript if you're more familiar with that. You would have to do roughly the same thing (which is what the form isn't doing) is create an array of the checkboxes where you specify that one or more of them aren't checked, and pass that into whatever action your form is set to use. Basically the form only says which boxes are checked, the un-checked one are completely disregarded. Commented Oct 13, 2017 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.