在Zend Framework 2中创建一个下拉列表

125*_*369 5 php zend-framework2 drop-down-menu

我知道这听起来更基本,我仍然想发布我的问题,因为它与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',
            ),
        ));
    }
}
Run Code Online (Sandbox Code Playgroud)

这就是以这种方式召唤的

<?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();
Run Code Online (Sandbox Code Playgroud)

如何为艺术家字段添加下拉列表,其中列表存储在关联数组中.自从我进入Zend Framework 2以来,我想要专家的建议.我已经关注了上一篇文章,但对我来说有点不清楚.

tho*_*ter 11

这是为静态选项执行此操作的一种方法.

....

$this->add(array(
    'type' => 'Zend\Form\Element\Select',
    'name' => 'number'
    'options' array(
        'options' => array( '1' => 'one', '2', 'two' )
    )
));
Run Code Online (Sandbox Code Playgroud)

被警告....

因为您在构造函数中创建表单,所以您将无法访问ServiceManger.如果要从数据库填充,这可能会导致问题.

让我们试试......

class AlbumForm extends Form implements ServiceManagerAwareInterface
{

public function __construct()
{
    ....

    $this->add(array(
        'type' => 'Zend\Form\Element\Select',
        'name' => 'number'
    ));

    ....
}
Run Code Online (Sandbox Code Playgroud)

....

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;
}
Run Code Online (Sandbox Code Playgroud)

但那不是很好,重新考虑......

扩展表单以便您可以创建表单是不对的.我们没有创建新类型的表单,我们只是设置一个表单.这需要一个工厂.此外,在这里使用工厂的优点是我们可以以我们可以使用服务管理器来提供它的方式进行设置,这样服务管理器可以注入自己而不是我们从控制器手动执行.另一个优点是,只要我们有服务管理器,我们就可以调用此表单.

值得一提的另一点是,在有意义的地方,我认为最好从控制器中取出代码.控制器不是脚本转储,所以让对象看起来很好.我想说的是,用一个对象注入它需要的对象是好的,但是从控制器那里传递数据是不可能的,因为它创造了太多的依赖.不要用勺子从控制器中取出物品,注入勺子.

无论如何,太多的代码咆哮......

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

调节器

<?php

class FooController
{
    ...
    protected function getForm()
    {
        if ( is_null($this->form) ) {
            $this->form =
                $this->getServiceManager()->get('MySpankingFormService');
        }
        return $this->form;
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

module.config.php

...
'service_manager' => array (
        'factories' => array (
            ...
            'MySpankingFormService'
                => 'MyNameSpacing\Foo\MySpankingFormService',
            ...
Run Code Online (Sandbox Code Playgroud)

  • ZF2形式比ZF1更令人困惑 (7认同)