Zend Framework 2,事件管理器,SharedManager,Form Init

时间:2014-07-30 08:18:26

标签: zend-framework2

我已将事件添加到onBootstrap()

$e->getApplication()->getEventManager()->getSharedManager()->attach('\Application\Form\Presentation\Edit', 'init', function($e) {
        exit(print_r('<pre>') . var_dump($e));
    }, 1);

如何在调用表单方法init后运行此事件?

1 个答案:

答案 0 :(得分:0)

我有解决方案,

在onBootstrap()

    $services = $e->getApplication()->getServiceManager();
    $sharedEventManager = $e->getApplication()->getEventManager()->getSharedManager();

    $sharedEventManager->attach('PresentationEdit','init', function($event) use ($services) {
        $form = $event->getTarget();
        $form->add(array(
            'type' => '\Zend\Form\Element\Text',
            'name' => 'title2',
            'attributes' => array(
               'type'  => 'text',
               'class' => 'form-control',
                'autocomplete' => 'off',
            ),
            'options' => array(
                'label' => 'presentation.title',
            )
        ));
    }, 100);

表格:

use Zend\EventManager\EventManager;

在init方法中:

$event = new EventManager('PresentationEdit');
$event->trigger('init', $this);

它有效,但这是正确的方法吗?