Php Symfony在提交数据之前使用表单事件更改数据
我已经准备好了表单EventPhp 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;
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);
}