在FormEvents :: SUBMIT中获取用户输入数据

时间:2014-11-18 18:45:06

标签: forms symfony events form-submit

我正在寻找一种在formEvents::SUBMIT事件中获取用户数据的方法。

我可以使用

formEvents::PRE_SUBMIT中获取它
$builder->addEventListener(FormEvents::PRE_SUBMIT, function() {
    //...
    $userInput = $event->getData());
    //...
});

我可以使用

formEvents::POST_SUBMIT中获取它
$builder->addEventListener(FormEvents::POST_SUBMIT, function() {
    //...
    $userInput = $event->getForm()->getViewData()
    //...
});

但是在formEvents::SUBMIT我找不到哪里,虽然我读到它可以访问: Symfony2: dynamic generation of embedded form

我可以使用与PRE_SUBMIT相同的方法找到其他项目获取数据,例如: https://github.com/Klerik/web-dersen/blob/65ba1bafa574dc2bda6dd2bb738fe0d33a06bf71/src/Catalog/FilmsBundle/Form/EventListener/UploadFileSubscriber.php

或那里: https://github.com/alexandresalome/bros/blob/59c111eb481c2fd672f3646390455ddad65dd800/src/Bros/ServerBundle/Form/Type/BrowserType.php

虽然在我的情况下我使用不符合规范化的用户输入进行测试 - $event->getForm()['field']->getData()发送一个没有被拒绝的用户数据的对象。 我还想检索用户输入: *相应地更改(测试数据有效性)我当前的表格(不能在POST_SUBMIT中完成), *使用我的表单相关对象(PRE_SUBMIT中没有)

修改

以下是......类型类的相关部分:

public function buildForm(FormBuilderInterface $builder, Array $options)
{
    $adminUserQuery = $options['admin_user_query'];

    $builder
        ->add('admin_user', 'model', Array( //using Propel as ORM
            'label' => FALSE,
            'multiple' => FALSE,
            'expanded' => FALSE,
            'class' => 'App\\CoreBundle\\Model\\AdminUser',
            'query' => $adminUserQuery,
            'empty_value' => 'form.placeholder.admin_user',
            'property' => 'email',
        ));
    //...

    $builder->addEventListener(
        FormEvents::PRE_SUBMIT, function (FormEvent $formEvent)
    {
        var_dump($formEvent->getData(), '---'); // prints: array(1) { ["admin_user"]=> string(17) "test@email.test" }
        var_dump($formEvent->getData()['admin_user'], '---'); // prints: string(15) "test@email.test"
        //...
    });

    $builder->addEventListener(
        FormEvents::SUBMIT, function (FormEvent $formEvent)
    {
        var_dump($formEvent->getData()); // prints: object(App\CoreBundle\Model\EventUserRole)#1659 {...}
        var_dump($formEvent->getData()->getAdminUser()); // prints: NULL

        //...
    }
    );
}

任何人?

1 个答案:

答案 0 :(得分:0)

在FormEvents :: Submit操作中,您可以执行此操作(假设您正在编辑用户):

$builder->addEventListener(FormEvents::SUBMIT, function ($event) {
    // Your Entity/Document
    $user = $event->getData();

    // Grab the actual form
    $form = $event->getForm();

    // ...
});