Symfony2:如何在提交后删除侦听器中的表单字段(Symfony2 : How to remove form field in listener, after submit)

编程入门 行业动态 更新时间:2024-10-28 16:28:46
Symfony2:如何在提交后删除侦听器中的表单字段(Symfony2 : How to remove form field in listener, after submit)

我遇到了FormType中的问题。 基于“Product”实体,this from有一个名为“ProductCriteria”的字段,它是一个基于“ProductCriteriaType”(Form)的集合。

我的productCriteria Model包含一个“已删除”属性,用于处理此实体的逻辑抑制。 如果设置为true,那么我不希望productCriteria表单类型显示在产品表单类型中。 如果它设置为false,那么我想在产品表单类型中显示它,你得到它。

我在我的表单的PRE_SET_DATA事件中写了这个,这非常好用:

$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) { $product = $event->getData(); /** @var PersistentCollection $productCriteriasFields */ $productCriteriasFields = $product->getProductCriterias(); /** @var ProductCriteria $productCriteria */ foreach($productCriteriasFields as $productCriteria){ if($productCriteria->getDeleted()){ $productCriteriasFields->removeElement($productCriteria); } } });

我的字段在表单中设置为true / false,当我访问FormType时,它不会按预期显示delete-true。

唯一的问题是:当我刚修改'删除'值时,控制器在表单isValid()时不会重定向到另一个页面。 它只是再次呈现FormType,表单DOESNT包含我的修改(但它们在DB中!)。 如果我刷新页面(F5),我就有了我想要的修改。 怎么解决这个? 我试图添加一个'POST_SUBMIT'函数,在这样的表单字段上工作:

$builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event){ /** @var Product $data */ $product = $event->getForm()->getData(); //this was supposed to do the trick $form = $event->getForm(); /** @var ProductCriteria $productCriteria */ for($i = 0; $i < count($product->getProductCriterias()); $i++){ if($product->getProductCriterias()[$i]->getDeleted()){ ladybug_dump("unsetting one field ! "); unset($form->get('productCriterias')[$i]); } }

但没有任何作用。 任何想法? 非常感谢。

编辑控制器的代码(它是一个抽象的代码:我的项目的archi,不是一个选择):

/** @var EditableEntityInterface $entity */ $entity = $this->entityService->getEntityById($itemId); $form = $this->createForm('admin' . $this->entityService->getEntityName(), $entity); $form->handleRequest($this->get('request')); $submitSuccess = false; if ($form->isValid()) { $this->entityService->save($form->getData()); $submitSuccess = true; } $template = $this->getActionTemplate($this->entityService->getEditTemplate()); return $this->render($template, array( 'item' => $entity, 'form' => $form->createView(), 'entityService' => $this->entityService, 'submitSuccess' => $submitSuccess ));

Im facing a problem in a FormType I have. based on the "Product" entity, this from has a field called "ProductCriteria", which is a collection based on "ProductCriteriaType" (Form).

My productCriteria Model hold a "deleted" attribute that handles the logic suppression of this entity. If it is set to true, then i dont want the productCriteria form type to be display in the product Form Type. If it is set to false, then i want to display it in the product Form Type, you get it.

I have written this in the PRE_SET_DATA event of my form, which works incredibly well:

$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) { $product = $event->getData(); /** @var PersistentCollection $productCriteriasFields */ $productCriteriasFields = $product->getProductCriterias(); /** @var ProductCriteria $productCriteria */ foreach($productCriteriasFields as $productCriteria){ if($productCriteria->getDeleted()){ $productCriteriasFields->removeElement($productCriteria); } } });

My fields are set to true/false in the form and when I access the FormType it doesnt display the delete-true one as expected.

THE ONLY PROBLEM IS: When I just modified a 'deleted' value, the controller doesnt redirect to another page when the form isValid(). It just renders the FormType again, and the form DOESNT CONTAIN MY MODIFICATIONS (but they are in DB !). If I refresh the page (F5), I have in deed the modifications I want. How to solve this ? I tried to add a 'POST_SUBMIT' function, working on the form fields like this:

$builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event){ /** @var Product $data */ $product = $event->getForm()->getData(); //this was supposed to do the trick $form = $event->getForm(); /** @var ProductCriteria $productCriteria */ for($i = 0; $i < count($product->getProductCriterias()); $i++){ if($product->getProductCriterias()[$i]->getDeleted()){ ladybug_dump("unsetting one field ! "); unset($form->get('productCriterias')[$i]); } }

But nothing works. Any idea? Thanks a LOT.

EDIT Controller's code (it is an abstracted one: archi of my project, not a choice):

/** @var EditableEntityInterface $entity */ $entity = $this->entityService->getEntityById($itemId); $form = $this->createForm('admin' . $this->entityService->getEntityName(), $entity); $form->handleRequest($this->get('request')); $submitSuccess = false; if ($form->isValid()) { $this->entityService->save($form->getData()); $submitSuccess = true; } $template = $this->getActionTemplate($this->entityService->getEditTemplate()); return $this->render($template, array( 'item' => $entity, 'form' => $form->createView(), 'entityService' => $this->entityService, 'submitSuccess' => $submitSuccess ));

最满意答案

好的,所以我终于有了工作! 问题出在我修改的控制器代码中:

$form->handleRequest($this->get('request')); $submitSuccess = false; if ($form->isValid()) { $this->entityService->save($form->getData()); $submitSuccess = true; // Data sync hardened ! $entity = $this->entityService->getEntityById($itemId); $form = $this->createForm('admin' . $this->entityService->getEntityName(), $entity); }

我强迫表单由我们保存的实体重新补充,以确保它不会占用前一个$请求中的先前数据。 但仍然......这是令人难以置信的,我无法通过使用表单事件来实现这一点。 该文档非常糟糕,只展示了非常简单的例子。 对于那些可能在表单事件上提供更多信息的人,我可以推荐这个: http : //symfony.com/doc/current/components/form/form_events.html#component-form-event-table并查看这个类在核心:Symfony \ Component \ Form \ Extension \ Core \ EventListener \ ResizeFormListener.php几乎所有你可以在表格上使用主FormEvents做的事情。

Okay so i finally got the thing to work !! The problem was in deed from my controller code in which i modified:

$form->handleRequest($this->get('request')); $submitSuccess = false; if ($form->isValid()) { $this->entityService->save($form->getData()); $submitSuccess = true; // Data sync hardened ! $entity = $this->entityService->getEntityById($itemId); $form = $this->createForm('admin' . $this->entityService->getEntityName(), $entity); }

I forced the form to be re-hydrated by the entity we just save, to ensure it doesnt take previous datas in the previous $request. But still... this is incredible I couldn't achieve this by using form event. The doc is horribly poor, and shows only very simple examples. For those who might neet more info on form event, i can recommand this: http://symfony.com/doc/current/components/form/form_events.html#component-form-event-table and have a look at this class in the core: Symfony\Component\Form\Extension\Core\EventListener\ResizeFormListener.php thats pretty much everything you can do on forms with the main FormEvents.

更多推荐

本文发布于:2023-08-01 00:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1351782.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   表单   器中   如何在   提交后

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!