Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony在提交数据之前使用表单事件更改数据_Php_Symfony - Fatal编程技术网

Php Symfony在提交数据之前使用表单事件更改数据

Php Symfony在提交数据之前使用表单事件更改数据,php,symfony,Php,Symfony,我已经准备好了表单EventFormEvents::PRE_SUBMIT namespace AppBundle\Form\EventListener; use Doctrine\ORM\EntityManager; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; use Symfony\Component\EventDispatcher\EventSubscriberInterface;

我已经准备好了表单Event
FormEvents::PRE_SUBMIT

namespace AppBundle\Form\EventListener;

use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;


class AddProfileFieldSubscriber implements EventSubscriberInterface
{
    protected $authorizationChecker;

    protected $em;

    function __construct(AuthorizationChecker $authorizationChecker, EntityManager $em)
    {
        $this->authorizationChecker = $authorizationChecker;
        $this->em = $em;
    }

    public static function getSubscribedEvents()
    {
        // Tells the dispatcher that you want to listen on the form.pre_set_data
        // event and that the preSetData method should be called.
        return array(
            FormEvents::PRE_SUBMIT => 'onPreSubmit'
        );
    }


    /**
     * @param FormEvent $event
     */
    public function onPreSubmit(FormEvent $event){

        $interestTags = $event->getData();
        $interestTags = $interestTags['interest'];
        foreach($interestTags as $key => $interestTag){
                $interestTags[$key] = "55";
            }
        }
}
在函数
onPreSubmit
中,如果我转储
$event
,我可以看到以下信息

我所要做的就是更改
,您可以看到红色箭头所指的位置,以便继续执行流程的其余部分,使用新的
,而不是旧的值


我使用的方法似乎可以更改值,但一旦我移出foreach循环,旧值就会保留下来,我需要做些什么,以便在剩下的过程中,将
qqq
替换为
444
,您需要使用所修改的数组设置事件数据:

public function onPreSubmit(FormEvent $event){

        $interestTags = $event->getData();
        $interestTags = $interestTags['interest'];
        foreach($interestTags as $key => $interestTag){
                $interestTags[$key] = "55";
            }
        }
        $event->setData($interestTags);
}