FOSUserBundle从侦听器中的表单中获取数据

时间:2013-08-02 02:08:41

标签: symfony fosuserbundle symfony-2.3

我试图在我在侦听器中创建的响应中插入表单(电子邮件)中传递的数据,以确保响应是json对象。

我无法以任何方式从'事件中获取表单数据。

我想要的解决方案是什么?

public static function getSubscribedEvents()
{
    return array(
        FOSUserEvents::PROFILE_EDIT_SUCCESS => 'onProfileEditSuccess',
    );
}

public function onProfileEditSuccess(FormEvent $event)
{

    $response = new Response();
    $output = array('success' => true, 'new_mail' => $event); //event return empty object
    $response->headers->set('Content-Type', 'application/json');
    $response->setContent(json_encode($output));
    $event->setResponse($response);
}

我试着听完完成的事件,但不会让我改变回应!

1 个答案:

答案 0 :(得分:1)

您可以在$event->getForm()事件中使用PROFILE_EDIT_SUCCESS从$ event对象中获取表单。

FOS\UserBundle\Controller\ProfileController中的

$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);

访问电子邮件

$form = $event->getForm();
$email = $form['email']->getData();