我知道这听起来更基本,我仍然想发布我的问题,因为它与Zend Framework 2有关。我从Zend示例模块知道这个表单
namespace Album\Form;
use Zend\Form\Form;
class AlbumForm extends Form
{
public function __construct($name = null)
{
// we want to ignore the name passed
parent::__construct('album');
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'id',
'attributes' => array(
'type' => 'hidden',
),
));
$this->add(array(
'name' => 'artist',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Artist',
),
));
$this->add(array(
'name' => 'title',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Title',
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Go',
'id' => 'submitbutton',
),
));
}
}
这就是以这种方式召唤的
<?php
$form = $this->form;
$form->setAttribute('action', $this->url('album', array('action' => 'add')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('title'));
echo $this->formRow($form->get('artist'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
如何为艺术家字段添加下拉列表,其中列表存储在关联数组中。自从我进入Zend Framework 2以来,我想要专家的建议。我跟着this previous post,但我有点不清楚。
答案 0 :(得分:11)
这是静态选项的一种方法。
....
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'number'
'options' array(
'options' => array( '1' => 'one', '2', 'two' )
)
));
警告......
因为您在构造函数中创建表单,所以您将无法访问ServiceManger。如果要从数据库填充,这可能会导致问题。
让我们尝试类似......
class AlbumForm extends Form implements ServiceManagerAwareInterface
{
public function __construct()
{
....
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'number'
));
....
}
...
public function initFormOptions()
{
$this->get('number')->setAttribute('options', $this->getNumberOptions());
}
protected function getNumberOptions()
{
// or however you want to load the data in
$mapper = $this->getServiceManager()->get('NumberMapper');
return $mapper->getList();
}
public function getServiceManager()
{
if ( is_null($this->serviceManager) ) {
throw new Exception('The ServiceManager has not been set.');
}
return $this->serviceManager;
}
public function setServiceManager(ServiceManager $serviceManager)
{
$this->serviceManager = $serviceManager;
}
但那不是很好,重新考虑......
扩展表单以便您可以创建表单并不是很正确。我们没有创建新类型的表单,我们只是设置一个表单。这需要一个工厂。此外,在这里使用工厂的优点是我们可以以我们可以使用服务管理器来提供它的方式进行设置,这样服务管理器可以注入自己,而不是我们从控制器手动执行。另一个优点是,只要有服务管理器,我们就可以调用此表单。
值得做的另一点是,在有意义的地方,我认为从控制器中取出代码会更好。控制器不是脚本转储,所以让对象看起来很好。我想说的是,用一个对象注入它需要的对象是件好事,但是从控制器那里传递数据是不行的,因为它创造的太多了依赖。 不要从控制器中取出勺子,然后注入勺子。
无论如何,太多的代码咆哮......
class MySpankingFormService implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceManager )
{
$mySpankingNewForm = new Form;
// build that form baby,
// you have a service manager,
// inject it if you need to,
// otherwise just use it.
return $mySpankingNewForm;
}
}
控制器
<?php
class FooController
{
...
protected function getForm()
{
if ( is_null($this->form) ) {
$this->form =
$this->getServiceManager()->get('MySpankingFormService');
}
return $this->form;
}
...
}
module.config.php
...
'service_manager' => array (
'factories' => array (
...
'MySpankingFormService'
=> 'MyNameSpacing\Foo\MySpankingFormService',
...