Multiple $form->checkbox "checked" issue

Permalink
I have multiple checkboxes created like this in a block:
$form = Loader::helper('form');
echo $form->checkbox('mycheckbox-'.$count, $list['id'], (in_array($list['id'], $ListIdsArray)), array('name'=>'ListIdsArray[]')).$list['name']."<br />";


In the block's edit mode, the "in_array()" function does not check the checkbox when true.

In order to make this work, I added the following statement before executing $form->checkbox and it seems to work:
$_SERVER['REQUEST_METHOD'] = '';


I couldn't get this to work any other way. Is this the right way to resolve this?

BlueFractals
 
JohntheFish replied on at Permalink Best Answer Reply 1 Attachment
JohntheFish
This is a long term bug of the form helper. It tries to make any form entries sticky - useful for failed validations. However, in the case of an array of checkboxes it gets it badly wrong.

I wouldn't zero out the whole request object. There is too big a chance of other code needing it after you have destroyed it. Just the part that contains the checkboxes.

(Not sure if this is the right code - a while since I have done this)
unset($_REQUEST['my_checkbox_array']);


You can also use a redirect after a single page save method to clear a form.

Personally, I have my own hacked version of the form helper that has options to disable persistence and ids (no other changes).
$form = Loader::helper('improved_form', 'my_package');
$form->disableFormContinuity();
$form->disableIdOutput();

There are corresponding enables, so it can be switched off and on again.

The form helper is in such a mess of entropy that the core are scared of any changes other than critical bug fixes, because so much code is out there that depends on those quirks.

There has been talk of a new and better form helper that could coexist with it.