PEAR quickform2 checkbox

How do I set a checkbox element which is already checked in quickform2?

Docs suggest it goes in as $data array, though I cannot confirm as I have not tested it.

This post I found then suggests you have to implicitly tell is to be checked, using chaining, beware though the Q was posed with Quickform and not Quickform2 - so may not applicable.

Like I say, all rather much guesswork, but I’m quite interested by QF2.


Ah, yes - confirmation and a link to the correct part of the api docs.

Spent ages with this and nothing seems to work. I can set the value in $data array but the checkbox remains unchecked.
Web searches suggest this

$promos = $fieldset->addElement('checkbox', 'promos', array('value' => '6', 'checked' => 'checked'), array('label' => 'I would like to receive emails'));

but it still doesn’t check the box.

Think its time for an alternative approach. Anybody able to recommend an alternative to quickform which will allow me to set rules and validate the form in a similar manner but without these problems?

So did you try the solution I posted for you?

append (chain) this method:

[your declaration and then]->setChecked(true);

// maybe if I show you exactly

$promos = $fieldset->addElement('checkbox', 'promos', 
                array('value' => '6', 'checked' => 'checked'), 
                array('label' => 'I would like to receive emails'))->setChecked(true); 

Yes, tried that and it returns an error

Fatal error: Call to undefined method HTML_QuickForm2_Element_InputCheckbox::setChecked()

Cant figure this out as I have

 require_once 'HTML/QuickForm2/Element/InputCheckbox.php';

at the top of the page so would have expected it to have found it.

I have also tried instantiating and echo a new object

$checkbox = new HTML_QuickForm2_Element_InputCheckbox(null, null, array('checked' => 'checked'));
        echo $checkbox;

This returns the checkbox but it is still unchecked.
Any thoughts appreciated.

Sorry mate, I’m out of ideas and I just don’t have the time to download this and test it for you.

Anyone else?

At last this seems to be getting close to what I want giving achecked box with value 6

$checkbox = new HTML_QuickForm2_Element_InputCheckbox('promos', array('checked' => 'checked', 'value' => '6'));
        echo $checkbox;

Cups, I appreciate what you say about not having time…
Spent too long on this myself.