这可能很容易,但我是新手。
我正在尝试写一个简单的帖子&类别系统。我创建了一个类别模块,还发布了模块。
我想添加selectbox以选择要添加到其中的这个类别的页面。我不知道怎么做。任何人都可以帮助我吗?
答案 0 :(得分:1)
你只需要在表单中注入结果集并创建一个数组,以便在select选项中传递它。
<?php
namespace Mylib\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Mylib\Form\MyForm;
class MyControler extends AbstractActionController
{
private $myObjectTable;
public function getMyObjectTable(){
if(!$this->myObjectTable){
$this->myObjectTable = $this->getServiceLocator()
->get('MyLib\Model\MyObjectTable');
}
}
public function indexAction(){
$objects = $this->getMyObjectTable()->fetchall();
$form = new MyForm($objects);
$filter = new MyFormFilter();
$form->setInputFilter($filter->getInputFilter());
$form->setData($request->getPost());
if($form->isValid()){
// [...] here the post treatment
}
return array('form' =>$form);
}
}
并以您的形式:
<?php
namespace Mylib\Form;
class MyForm
{
public function __construct($objects){
$selectOptions = array();
foreach ($objects as $object) {
$selectOptions[$object->id]=$object->thePropertyIWantToList;
}
$this->add(array(
'name' => 'object_id',
'type' => 'Select',
'options' => array(
'value_options' => $selectOptions,
'label' => 'MySelectBoxLabel',
)
'attributes' => array(
'value' => 9, // to select a default value
)
));
//and the rest of the form...
$this->add(array(
'name' => 'submit',
'type' => 'Submit',
'attributes' => array(
'id' => 'submitbutton',
'value' => 'Go',
),
));
}
}