From: Alain Williams on
Trying to get to use checkboxes with quickform2.

A checkbox can be given more than once in a form with the same name:

http://www.w3.org/TR/html401/interact/forms.html#checkbox

If you do this you only see the value of the last one set in the form.
OK: set as an array name, eg:

$form->addElement('checkbox', 'vegetable[]', array('value' => 1), array('label' => 'carrot'));
$form->addElement('checkbox', 'vegetable[]', array('value' => 2), array('label' => 'pea'));
$form->addElement('checkbox', 'vegetable[]', array('value' => 3), array('label' => 'bean'));

PHP correctly interprets that resulting in the following in an array if carrot & bean are selected:
[0] => 1
[1] => 3
However, this does not appear in what $form->getValue() returns.

I could give them separate names as below -- but that makes processing harder:
$form->addElement('checkbox', 'vegetable1', array('value' => 1), array('label' => 'carrot'));
$form->addElement('checkbox', 'vegetable2', array('value' => 2), array('label' => 'pea'));
$form->addElement('checkbox', 'vegetable3', array('value' => 3), array('label' => 'bean'));


How am I supposed to do this ?

--
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
+44 (0) 787 668 0256 http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php
Past chairman of UKUUG: http://www.ukuug.org/
#include <std_disclaimer.h>
From: Alexey Borzov on
Hi Alain,

Alain Williams wrote:
> If you do this you only see the value of the last one set in the form.
> OK: set as an array name, eg:
>
> $form->addElement('checkbox', 'vegetable[]', array('value' => 1), array('label' => 'carrot'));
> $form->addElement('checkbox', 'vegetable[]', array('value' => 2), array('label' => 'pea'));
> $form->addElement('checkbox', 'vegetable[]', array('value' => 3), array('label' => 'bean'));
>
> PHP correctly interprets that resulting in the following in an array if carrot & bean are selected:
> [0] => 1
> [1] => 3
> However, this does not appear in what $form->getValue() returns.
>
> I could give them separate names as below -- but that makes processing harder:
> $form->addElement('checkbox', 'vegetable1', array('value' => 1), array('label' => 'carrot'));
> $form->addElement('checkbox', 'vegetable2', array('value' => 2), array('label' => 'pea'));
> $form->addElement('checkbox', 'vegetable3', array('value' => 3), array('label' => 'bean'));
>
>
> How am I supposed to do this ?
>

QuickForm2 is currently not smart enough to properly get the values for elements
named 'foo[]', unless such elements are multiselects where it appends '[]'
itself. A workaround is to do something along the lines of

$form->addElement('checkbox', 'vegetable[0]', array('value' => 1), array('label'
=> 'carrot'));
$form->addElement('checkbox', 'vegetable[1]', array('value' => 2), array('label'
=> 'pea'));
$form->addElement('checkbox', 'vegetable[2]', array('value' => 3), array('label'
=> 'bean'));


We had a bit of off-list discussion with Bertrand about allowing names with '[]'
but failed to reach a decision yet. There are some corner cases, consider

$form->addElement('checkbox', 'foo[]');
$form->addElement('text', 'foo[]');

for text element to properly get its submit value from submit DataSource, it
needs to know whether checkbox element was checked or not when the form was
submitted. This makes value-getting logic *very* convoluted.

Anyway, for a special case of checkboxes it may easily be allowed, since we can
just get an array from submit DataSources and check whether its value is in that
array. Can you please open a feature request for that?