Juan
Juan

Reputation: 178

zf2 Checkbox creation in ZendForm fails with array notation

I'm creating a Form and adding a checkbox with ZF2, but for some reason the options dont get sent when I use array notation.

So this:

class PageForm extends Form
{
  public function __construct($name = null)
  {
$checkbox = new Element\Checkbox('system');
    $checkbox   ->setLabel('System Page')
                ->setUseHiddenElement(true)
                ->setCheckedValue("1")
                ->setUncheckedValue("0");


    $this->add($checkbox);
}
}

Works correctly, but this:

     class PageForm extends Form
    {
      public function __construct($name = null)
      {
   $this->add(array(
             'type' => 'Checkbox',
             'name' => 'checkbox',
             'options' => array(
                     'label' => 'A checkbox',
                     'use_hidden_element' => true,
                     'checked_value' => 'good',
                     'unchecked_value' => 'bad'
             )
    }
    }

Creates the checkbox but without the checked/unchecked values, I'm wondering if I'm doing something wrong, or missing a step? (the example is straight from the documentation)

this is the code in the view:

$form = $this->form;
$form->setAttribute('action', $this->url('page', array('action' => 'add')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formCollection($form);
echo $this->form()->closeTag();

Thanks

Upvotes: 1

Views: 1431

Answers (1)

Andrew
Andrew

Reputation: 12809

Why not use Multicheckbox instead?

 $multiCheckbox = new Element\MultiCheckbox('multi-checkbox');
 $multiCheckbox->setLabel('What do you like ?');
 $multiCheckbox->setValueOptions(array(
         array(
                 '0' => 'Apple',
                 '1' => 'Orange',
                 '2' => 'Lemon'
         )
 ));
 $form = new Form('my-form');
 $form->add($multiCheckbox);

Upvotes: 1

Related Questions