2011-09-22 69 views
1

的HTML我的複選框列表設置默認值是這樣的:Zend的形式 - 多選框

<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> 

我試圖與選擇=選擇那些從數據庫中的1值了預填充。我認爲以下將起作用:

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

但它沒有。有沒有辦法做到這一點?

編輯

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); 
+0

,你可以加入你的窗體類的代碼?我想我知道問題出在哪裏,但我需要確定 –

回答

2

你的表單元素的名稱爲list[],這意味着,當你的箱子是檢查,你得到他們的價值:

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

$列表的價值將是陣列(1,2)

所以邏輯上這應該工作:

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

試試這個:

$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); 

$這當然:)的Zend_Form的