php - 使用 zfcuser 和自定义模块选择字段

标签 php zend-framework2 zfcuser

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

并添加一个选择字段,与官方表单文档中相同。

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

这是我的 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__,
                ),
            ),
        );
    }
}

请问有人可以帮助我理解这种行为并为我指明正确的方向吗?

最佳答案

您的代码没问题,问题在于用户模块提供的用于呈现表单的标准 View 。

注册.phtml

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

这是使用输入 View helepr 来渲染任何额外的元素。这可能应该是 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: ?>

关于php - 使用 zfcuser 和自定义模块选择字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14553577/

相关文章:

php - 自动加载器类 "Zend\Loader\ClassMapAutoLoader"未加载

zend-framework2 - Zend 框架 2 : How to properly replace Figlet with reCaptcha on zfcUser

php - 在 ZF2 中使用 ZfcUser 生成用户帐户

javascript - 如何刷新DIV而不是页面?

PHP、Yii 用户扩展

php - 如何在 Android 上换行通知内容

php - fatal error : Call to a member function getId() on null in doctrine

php - 克隆不能处理 zf2 Doctrine MongoOSEModule 中的嵌入式文档

zend-framework2 - 我将如何记录成功的登录

php - WebMatrix PHP 查询不起作用