ZF2 Form和Doctrine 2修改value_options

伤口

我在Zend Framework 2项目中使用Doctrine 2。现在,我已经创建了一个表单,并使用数据库中的值创建了一个下拉列表。我现在的问题是我想更改使用的值,而不是从存储库取回的值。好的,这里有一些代码可以更好地理解:

$this->add(
            array(
                    'type' => 'DoctrineModule\Form\Element\ObjectSelect',
                    'name' => 'county',

                    'options' => array(
                            'object_manager' => $this->getObjectManager(),
                            'label' => 'County',
                            'target_class'   => 'Advert\Entity\Geolocation',
                            'property'       => 'county',
                            'is_method' => true,
                            'empty_option' => '--- select county ---',
                            'value_options'=> function($targetEntity) {
                                $values = array($targetEntity->getCounty() => $targetEntity->getCounty());
                                return $values;
                            },

                            'find_method'        => array(
                                    'name'   => 'getCounties',
                            ),
                    ),
                    'allow_empty'  => true,
                    'required'     => false,
                    'attributes' => array(
                            'id' => 'county',
                            'multiple' => false,
                    )
            )
    ); 

我想将“选择”的值设置为县名而不是ID。我以为我需要一个需要数组的'value_options'。我像上面一样尝试过,但得到了

错误消息:传递给Zend \ Form \ Element \ Select :: setValueOptions()的参数1必须是数组类型,给定对象

这有可能吗?

亚历克斯

我打算建议修改您的代码,尽管在检查了ObjectSelect代码后,我很惊讶(据我所知),如果不扩展该类,实际上是不可能的。这是因为该值始终是从id生成的

我使用工厂(不带ObjectSelect创建了所有表单元素,特别是需要变化列表的复杂元素

替代解决方案

首先在存储库中创建一个新方法,该方法返回正确的数组。这将允许您在其他任何地方都需要重用相同的方法(不仅限于表单!)。

class FooRepository extends Repository
{
    public function getCounties()
    {
        // normal method unchanged, returns a collection
        // of counties
    }

    public function getCountiesAsArrayKeyedByCountyName()
    {
        $counties = array();
        foreach($this->getCounties() as $county) {
            $counties[$county->getName()] = $county->getName();
        }
        return $counties;
    }
}

接下来,创建一个自定义选择工厂,它将为您设置值选项。

namespace MyModule\Form\Element;

use Zend\Form\Element\Select;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\FactoryInterface;

class CountiesByNameSelectFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $formElementManager)
    {
        $element = new Select;
        $element->setValueOptions($this->loadValueOptions($formElementManager));

        // set other select options etc
        $element->setName('foo')
                ->setOptions(array('foo' => 'bar'));

        return $element;
    }

    protected function loadValueOptions(ServiceLocatorInterface $formElementManager)
    {
        $serviceManager = $formElementManager->getServiceLocator();
        $repository = $serviceManager->get('DoctrineObjectManager')->getRepository('Foo/Entity/Bar');

        return $repository->getCountiesAsArrayKeyedByCountyName();
    }

}

通过在Module.php或中添加新条目,向服务管理器注册新元素module.config.php

// Module.php
public function getFormElementConfig()
{
    return array(
        'factories' => array(
            'MyModule\Form\Element\CountiesByNameSelect'
                => 'MyModule\Form\Element\CountiesByNameSelectFactory',
        ),
    );
}

最后,更改表单并删除当前的select元素,然后添加新的select元素(使用您在服务管理器中注册的名称作为类型键)

$this->add(array(
    'name' => 'counties',
    'type' => 'MyModule\Form\Element\CountiesByNameSelect',
));

看起来似乎有很多代码(因为是这样),但是您将受益于它更加清晰的关注点分离,并且您现在可以在多种表单上重用该元素,并且只需要在一个位置进行配置即可。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

ZF2和Doctrine2:模拟EntityRepository :: findOneByField

来自分类Dev

ZF2 Doctrine 服务 - 多次调用

来自分类Dev

ZF2:试图了解Zend \ Form

来自分类Dev

ZF2水化器和阵列

来自分类Dev

ZF2中的Acl和RBAC

来自分类Dev

Zf2 HydratingResultSet和多个实体

来自分类Dev

ZF2导航和站点地图

来自分类Dev

Zf2 HydratingResultSet和多个实体

来自分类Dev

配置中的ZF2 Doctrine2 setFilterSchemaAssetsExpression设置

来自分类Dev

ZF2 Zend \ Log + Doctrine2

来自分类Dev

在ZF2项目中启用Doctrine 2缓存

来自分类Dev

配置中的ZF2 Doctrine2 setFilterSchemaAssetsExpression设置

来自分类Dev

使用ZF2更改Doctrine上的DefaultEntityListenerResolver

来自分类Dev

使用ZF2更改Doctrine上的DefaultEntityListenerResolver

来自分类Dev

使用ZF2和Doctrine2将SQL Server表转换为MySQL

来自分类Dev

Doctrine2访存更加优化和更快的计数方法或Zf2库

来自分类Dev

zf2和doctrine2 ObjectRadio从相关实体获取可变条件

来自分类Dev

编辑多体关联理论2和zf2

来自分类Dev

ZF2翻译

来自分类Dev

Doctrine2和zf2:将fetchAll()结果转换为JSON,然后将结果转换为数组,然后在ZF2中转换为json

来自分类Dev

在ZF2中使用Doctrine模块水化器和表单注释

来自分类Dev

如何使用命令行工具为ZF2和Doctrine生成“ getter / setter”?

来自分类Dev

设置和访问变量错误页面ZF2

来自分类Dev

ZF2中InputFilterAwareInterface和InputFilterProviderInterface之间的区别

来自分类Dev

ZF2从路线获取控制器和动作

来自分类Dev

ZF2表单验证-不重叠的日期和时间

来自分类Dev

ZF2 Mail UTF-8和Outlook Express

来自分类Dev

ZF2初始化程序和TranslatorAwareInterface

来自分类Dev

ZF2从路线获取控制器和动作

Related 相关文章

热门标签

归档