1

The HTML for my checkbox list is like this:

<dt id="list-label">
    <label for="list" class="optional">
        Choose which feeds to include in the mix
    </label>
</dt>

<dd id="list-element">
    <label for="list-1">
        <input type="checkbox" name="list[]" id="list-1" value="1">Marko Polo
    </label>

    <br />

    <label for="list-2">
        <input type="checkbox" name="list[]" id="list-2" value="2">Jeano Polo
    </label>
</dd>

I'm trying to pre-populate them with selected=selected for those with values of 1 from the database. I thought the following would work:

$form->setDefaults(array('list-1'=>1,'list-2'=>1));

But it doesn't. Is there a way to do this?

EDIT

Here is my form class code:

            $model = new Admin_Model_Db();
            $model->setTableName('graduates');
            $gradA = $model->getAllGraduates();

            foreach ($gradA as $grad){
                if (!empty($grad['twitter'])){
                    $twitter[$grad['id']] = $grad['firstname'] . ' ' . $grad['lastname'];
                }
            }

            $list = $this->CreateElement('multicheckbox', 'list')
                                  ->setLabel('Choose which feeds to include in the mix')
                                  ->setRequired(false)
                                  ->setMultiOptions($twitter);
1
  • could you join your form class code ? I think i know where the problem is but i need to see it to be sure Commented Sep 22, 2011 at 19:23

2 Answers 2

5

Try this:

$all_record = array('k1'=>'v1', 'k2'=>'v2');
$checked_values = aray('k1');

$checkbox = new Zend_Form_Element_MultiCheckbox('id', 
     array('multiOptions' => $all_records)
);

$checkbox->setValue($checked_values);

// ...

$this->addElement($checkbox);

$this is Zend_Form of course :)

Sign up to request clarification or add additional context in comments.

Comments

2

Your form element name is list[], this means that when your boxes are checked and you get their values this way :

 $list = $form->getElement('list')->getValue();

$list's value will be array(1,2)

So logically this should work :

$form->setDefaults(array('list'=>array(1,2));
//if not, try with strings instead of integers array('1','2') 

Comments

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.