本文介绍了在FormEvents :: 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 我不能找到哪里,虽然我看到它可以访问:



我可以使用与 PRE_SUBMIT 相同的方法找到其他项目获取数据,例如这里:




https ://github.com/alexandresalome/bros/bl对我有用[0]规范化 - $ 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(//使用Propel作为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)
[email protected]}
var_dump ($ formEvent-> getData()['admin_user'],'---'); // prints:string(15)[email protected]
// ...
});

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

// ...
}
);
}

任何人?

解决方案

在FormEvents :: Submit操作中,您可以这样做(假设您正在编辑用户):

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

//抓住实际的形式
$ form = $ event-> getForm();

// ...
});


I am looking for a way to get user data in the formEvents::SUBMIT event.

I can get it in formEvents::PRE_SUBMIT using

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

I can get it in formEvents::POST_SUBMIT using

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

But in formEvents::SUBMIT I cannot find where, though I read it can be accessed:Symfony2: dynamic generation of embedded form

I can find other project getting data using the same method as PRE_SUBMIT, for instance here:https://github.com/Klerik/web-dersen/blob/65ba1bafa574dc2bda6dd2bb738fe0d33a06bf71/src/Catalog/FilmsBundle/Form/EventListener/UploadFileSubscriber.php

or there:https://github.com/alexandresalome/bros/blob/59c111eb481c2fd672f3646390455ddad65dd800/src/Bros/ServerBundle/Form/Type/BrowserType.php

Though in my situation I test with user input that does not fit the Normalisation - $event->getForm()['field']->getData() sends an object without the user data which have been refused.Still I want to retrieve user input to:* change accordingly (testing data validity) my current form (which cannot be done in POST_SUBMIT),* use my form related object (not available in PRE_SUBMIT)

EDIT

Here are the relevant parts of the ...Type Class :

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) "[email protected]" }
        var_dump($formEvent->getData()['admin_user'], '---'); // prints: string(15) "[email protected]"
        //...
    });

    $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

        //...
    }
    );
}

Anyone ?

解决方案

In the FormEvents::Submit action, you can do this (assuming you are editing a User):

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

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

    // ...
});

这篇关于在FormEvents :: SUBMIT中获取用户输入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 10:33