How to use a custom form view helper in Zend Framework 2?

后端 未结 2 1700
天涯浪人
天涯浪人 2021-01-13 01:26

I wrote a form view helper, that extends the Zend\\Form\\View\\Helper\\FormMultiCheckbox and overwrites its renderOptions(...) method:



        
相关标签:
2条回答
  • 2021-01-13 02:08

    here's an example of overriding a view helper:

    http://ctrl-f5.net/php/zf2-servicemanager-custom-viewhelpers/


    Example:

    class Module {
    
        public function onBootstrap(MvcEvent $mvcEvent)
        {
            $application = $mvcEvent->getApplication();
            $serviceManager = $application->getServiceManager();
            $viewHelperManager = $serviceManager->get('ViewHelperManager');
            $viewHelperManager->setInvokableClass('formmulticheckbox', 'MyNamespace\Form\View\Helper\FormMultiCheckbox');
        }
        ...
    }
    
    0 讨论(0)
  • 2021-01-13 02:28

    Although Andrews answer works, it's not necessary, just use the default view helper name and map it to your helper class, the application will then use your helper instead

    public function getViewHelperConfig() {
        return array(
            'invokables' => array(
                'formmulticheckbox' => 'MyNamespace\Form\View\Helper\FormMultiCheckbox',
            ),                
        );
    }
    
    0 讨论(0)
提交回复
热议问题