XF 2.0 Unchecked checkbox field not returned by form?

keithg

Member
Maybe I am doing something silly?

If I check a checkbox I see the data value 1 returned by the form. If I uncheck the checkbox no field value is returned. I would expect a value of 0 to be returned.

This is an issue since I have added some fields to the forum_edit template (a few are checkboxes) for a related entity. After saving a record I can not clear any checkbox field that was once checked.

The set of new fields are grouped into the same array to allow my saveTypeData code to not have to know about the individual fields. A checked checkbox gives a field value of 1 which gets coppied to the entity by a call to setupEntityInput (which really just does a bulkset). An unchecked checkbox is not present in the array of fields and hence the value of that field in the existing entity is never changed from set to unset.

Searching the forum I found 1 vague hint that this is working as designed: unset checkboxes are not returned by the form. But there was no info on how to remedy / handle this. The normal form / entity system obviously handles this since I can uncheck other check boxes and the underlying field gets unset. Is there some catchall logic somewhere that provides 0 values for missing fields?

I also have not been able to locate any documentation about form tags like checkbox and checkbox row.

Cheers...
 
It’s not actually an XF thing, it’s just simply that the browser and/or server omits it entirely.

If you need a value (and the absence of a value is not enough to satisfy the code) then you can use a hidden form value set to 0 for this before the checkbox element.
 
It’s not actually an XF thing, it’s just simply that the browser and/or server omits it entirely.

If you need a value (and the absence of a value is not enough to satisfy the code) then you can use a hidden form value set to 0 for this before the checkbox element.
Thanks for the quick feedback.

Based upon that I can confirm that the browser is not returning the field. So working as expected.

Any hints on how the normal entity stuff provides "unchecked" value for missing fields?
 
...
If you need a value (and the absence of a value is not enough to satisfy the code) then you can use a hidden form value set to 0 for this before the checkbox element.
This was the hint. I misunderstood the response since I am mostly a backend person...

Based upon the info here: https://stackoverflow.com/questions/1809494/post-unchecked-html-checkboxes

I altered my template to include the following before my checkbox field:
<xf:hiddenval name="pforum[paidy]">0</xf:hiddenval>

and now the browser passes back a 0 if the checkbox is not checked. Note that the browser passes back both fields (with the same name) if the checkbox is checked. The internal XF routine provides the last value for fields of the same name.

Thanks!
 
Top Bottom