2
votes

I've started last week learning ZF2. I've succesfully installed the skeleton application and the ZfcUser module. I decided to edit the registration form as suggested in the ZfcUser Wiki: https://github.com/ZF-Commons/ZfcUser/wiki/How-to-modify-the-form-objects-used-by-ZfcUser

and add a select field, The same you can find in the official form Docs.

But the select is not displayed correctly as it's rendered like an input field. You can see here an example.

Here my 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__,
                ),
            ),
        );
    }
}

Please, can anyone help me understanding this behaviour and point me in the right direction?

1

1 Answers

4
votes

You code is fine, the issue is with the standard view provided by the user module for rendering the form.

register.phtml

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

This is using the input view helepr to render any extra element. This should probably be the formElement helper which is a generic helper to render any element depending on it's type:

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

that will work a charm, but It would probably be best to add in an extra check such as:

<?php elseif ($element instanceof Zend\Form\Element\Select): ?>
        <dd><?php echo $this->formSelect($element) . $this->formElementErrors($element) ?></dd>
<?php else: ?>