在Zend Framework 2中创建一个下拉列表 [英] Create a drop down list in Zend Framework 2

查看:91
本文介绍了在Zend Framework 2中创建一个下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这听起来更基础,但我仍然想发布我的问题,因为它与Zend Framework 2有关。我从Zend示例模块中了解了这个表单。

  namespace Album\Form; 

使用Zend\Form\Form;

class AlbumForm extends Form
{
public function __construct($ name = null)
{
//我们要忽略通过的名称
parent :: __ construct('album');
$ this-> setAttribute('method','post');
$ this-> add(array(
'name'=>'id',
'attributes'=> array(
'type'=>隐藏',
),
));
$ this-> add(array(
'name'=>'artist',
'attributes'=> array(
'type'=>文本',
),
'options'=>数组(
'label'=>'艺术家',
),
));
$ this-> add(array(
'name'=>'title',
'attributes'=> array(
'type'=>文本',
),
'options'=>数组(
'label'=>'标题',
),
));
$ this-> add(array(
'name'=>'submit',
'attributes'=>数组(
'type'=>提交',
'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以来,我想要专家的建议。我遵循了此前的帖子但是对我来说有点不清楚。

解决方案

这是静态选项的一种方法。 p>



  .... 

$ this-> add(array(
'type'=>'Zend\Form\Element\Select',
'name'=>'number'
'options'array b $ b'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());
}

保护函数getNumberOptions()
{
//或者你想加载数据
$ mapper = $ this-> getServiceManager() - >获得( 'NumberMapper');
return $ mapper-> getList();
}

public function getServiceManager()
{
if(is_null($ this-> serviceManager)){
throw new Exception('The ServiceManager尚未设置。
}

return $ this-> serviceManager;
}

public function setServiceManager(ServiceManager $ serviceManager)
{
$ this-> serviceManager = $ serviceManager;
}

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



扩展表单以便您可以创建表单是不正确的。我们不是创建一种新的形式,我们只是设置一个表单。这需要一个工厂。此外,在这里使用工厂的优点在于,我们可以通过这种方式来设置服务管理器来提供服务,这样服务管理员可以自己进行注入,而不是从控制器手动注入。另外一个优点是,只要我们有服务经理,我们就可以调用这个表单。



另外值得一提的是,在理解的地方,我认为最好采取代码的控制器。控制器不是一个脚本转储,所以很高兴有自己的对象。我想说的是,将对象注入到需要的对象中是很好的,但是从控制器传递数据是不正确的,因为它创建了太多的依赖关系。



无论如何,太多了更多的代码...

  class MySpankingFormService实现FactoryInterface 
{
public function createService(ServiceLocatorInterface $ serviceManager)
{
$ mySpankingNewForm = new Form;

//构建宝贝,
//你有一个服务经理,
//如果你需要,注入它
//否则只是使用它。

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'=>数组(
'工厂'=>数组(
...
'MySpankingFormService'
=>'MyNameSpacing\Foo\MySpankingFormService',
。 ..


I know this sounds much more basic, still I want to post my question as it is related to Zend Framework 2. I know this form from the Zend example module

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',
            ),
        ));
    }
}

And this is called in this fashion

<?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();

How can I add a drop down list for the artist field where the list is stored in an associative array. Since Im getting into Zend Framework 2, I wanted the suggestions from the experts. I have followed this previous post but it was somewhat unclear to me.

解决方案

This is one way to do it for static options.

....

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

Be warned....

Because you are creating the form within a constructor you will not have access the ServiceManger. This could cause a problem if you want to populate from a database.

Lets try something like...

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

But that's not great, rethink...

Extending the Form so that you can create a form isn't quite right. We are not creating a new type of form, we are just setting up a form. This calls for a factory. Also, the advantages of using a factory here are that we can set it up in a way in which we can use the service manager to serve it up, that way the service manager can inject itself instead of us doing in manually from the controller. Another advantage is that we can invoke this form whenever we have the service manager.

Another point worth making is that where it makes sense, I think it's better to take code out of the controller. The controller is not a script dump so it's nice to have objects look after themselves. What I'm trying to say is that it's good to inject an object with objects it needs, but it's not okay to just hand it the data from the controller because it creates too much of a dependency. Don't spoon feed objects from the controller, inject the spoon.

Anyway, too much rant more code...

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

controller

<?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',
            ...

这篇关于在Zend Framework 2中创建一个下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆