5

我知道这听起来更基本,但我仍然想发布我的问题,因为它与 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 以来,我希望得到专家的建议。我已经关注了上一篇文章,但对我来说有点不清楚。

4

1 回答 1

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;
    }
    ...
}

模块.config.php

...
'service_manager' => array (
        'factories' => array (
            ...
            'MySpankingFormService'
                => 'MyNameSpacing\Foo\MySpankingFormService',
            ...
于 2012-09-17T14:56:39.247 回答