1

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

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

This works too thanks, do you know if there is any reason the regular check-box doesn't work with array notation?
maybe because you are yusing a collections view helper to render? try using the formCheckbox helper..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.