Search code examples
zend-framework2zfcuser

extending zfcuser with custom radio fields


i am using ZFcuser with zendframework 2 and want to extend the registration form to enable me to add a radio field.

i followed the tutorial here

it works ok and i am able to render and obtain the values for imput tags. the problem comes when i try to render a radio button. it only renders one of the values.

here is my code:

   public function onBootstrap(MVCEvent $e)
    {
        $eventManager = $e->getApplication()->getEventManager();
        $em           = $eventManager->getSharedManager();
        $em->attach(
                'ZfcUser\Form\RegisterFilter',
                'init',
                function($e)
                {
                    $filter = $e->getTarget();

                    $filter->add(array(
                    'name' => 'accept',
                    'required' => FALSE
        ));           
                }
            );
            // custom form fields

            $em->attach(
                'ZfcUser\Form\Register',
                'init',
                function($e)
                {
                    /* @var $form \ZfcUser\Form\Register */
                    $form = $e->getTarget();
                    $form->add(
                        array(
                            'name' => 'username',
                            'options' => array(
                                'label' => 'Username',
                            ),
                            'attributes' => array(
                                'type'  => 'text',
                            ),
                        )
                    );


              $form->add(array(
                    'type' => 'Zend\Form\Element\radio',
                    'name' => 'terms',
                    'options' => array(
                        'label' => 'Accept Terms',
                        'value_options' => array(
                            '1' => 'Yes',
                            '0' => 'No',
                        ),
                    ),

                ));
                }
            );

            $zfcServiceEvents = $e->getApplication()->getServiceManager()->get('zfcuser_user_service')->getEventManager();

            $zfcServiceEvents->attach('register', function($e) {
                $form = $e->getParam('form');
                $user = $e->getParam('user');

the HTML

the html from this output only render 1 input tag; i.e

<input type="radio" value="" name="terms">

however, i need to render two radio buttons; one for the yes, and one for the no

does anyone have any ideas how to do this; i really appreciate your help


Solution

  • This is a limitation of the current ZfcUser(v. 1.x), but is something that will be fixed in the coming version(v. 2.x). In the meantime: overwrite the standard view with your own.