2

我上周开始学习ZF2。我已经成功安装了骨架应用程序和 ZfcUser 模块。我决定按照 ZfcUser Wiki 中的建议编辑注册表单: https ://github.com/ZF-Commons/ZfcUser/wiki/How-to-modify-the-form-objects-used-by-ZfcUser

并添加一个选择字段,您可以在官方表单文档中找到相同的字段。

但是选择没有正确显示,因为它像输入字段一样呈现。你可以 在这里看到一个例子。

这是我的 Application/Module.php

<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Application;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\Form\Form;

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        $e->getApplication()->getServiceManager()->get('translator');
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);

        $events = $e->getApplication()->getEventManager()->getSharedManager();
        $events->attach('ZfcUser\Form\Register','init', function($e) {
            $form = $e->getTarget();
            $form->add(array(
                    'type' => 'Zend\Form\Element\Select',
                    'options' => array(
                            'label' => 'Which is your mother tongue?',
                            'value_options' => array(
                                    '0' => 'French',
                                    '1' => 'English',
                                    '2' => 'Japanese',
                                    '3' => 'Chinese',
                            ),
                    ),
                    'name' => 'language'
            ));         
        });
        $events->attach('ZfcUser\Form\RegisterFilter','init', function($e) {
            $filter = $e->getTarget();
            // Do what you please with the filter instance ($filter)

        });
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
}

拜托,谁能帮助我理解这种行为并指出正确的方向?

4

1 回答 1

4

您的代码很好,问题在于用户模块提供的用于呈现表单的标准视图。

注册.phtml

<dd><?php echo $this->formInput($element) . $this->formElementErrors($element) ?></dd>

这是使用输入视图帮助渲染任何额外的元素。这可能应该是 formElement 助手,它是一个通用助手,可以根据其类型呈现任何元素:

<dd><?php echo $this->formElement($element) . $this->formElementErrors($element) ?></dd>

这会很有魅力,但最好添加一个额外的检查,例如:

<?php elseif ($element instanceof Zend\Form\Element\Select): ?>
        <dd><?php echo $this->formSelect($element) . $this->formElementErrors($element) ?></dd>
<?php else: ?>
于 2013-01-28T09:11:20.197 回答