How do I set a checkbox element which is already checked in quickform2?
Printable View
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.
EDIT
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 thisbut it still doesn't check the box.PHP Code:$promos = $fieldset->addElement('checkbox', 'promos', array('value' => '6', 'checked' => 'checked'), array('label' => 'I would like to receive emails'));
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);
PHP Code:// 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 haveat the top of the page so would have expected it to have found it.PHP Code:require_once 'HTML/QuickForm2/Element/InputCheckbox.php';
I have also tried instantiating and echo a new object
This returns the checkbox but it is still unchecked.PHP Code:$checkbox = new HTML_QuickForm2_Element_InputCheckbox(null, null, array('checked' => 'checked'));
echo $checkbox;
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 6Cups, I appreciate what you say about not having time.....PHP Code:$checkbox = new HTML_QuickForm2_Element_InputCheckbox('promos', array('checked' => 'checked', 'value' => '6'));
echo $checkbox;
Spent too long on this myself.