简体   繁体   English

是否需要,取决于一个字段-嵌入式表格Symfony2

[英]Required or not depending on one field - Embedded forms Symfony2

I'm doing a Forum bundle where you can add (or not) a vote to your subject. 我正在做一个论坛捆绑包,您可以在其中添加(或不添加)您的主题的投票。 The problem is that the embedded form (the question and the answers) has to be required only if a boolean is true. 问题是仅当布尔值为true时才需要嵌入形式(问题和答案)。

I first tried some solutions I found here and there on the internet but nothing worked. 我首先尝试了一些在Internet上和那里找到的解决方案,但是没有任何效果。 Then I created 2 forms, one with a vote, another without and I created a little switch with some JQuery. 然后,我创建了2种形式,一种具有表决权,另一种没有表决权,并且我使用一些JQuery创建了一个小开关。

The problem is that when I want to create a subject and only fill one form, HTML5 tells me there are fields required I didn't fill in the other form, it doesn't only send the form I'm trying to submit, it apparently submits both. 问题是,当我想创建一个主题并且仅填写一个表格时,HTML5告诉我有一些字段我没有填写其他表格,它不仅发送了我要提交的表格,显然都提交。

Do you know why and how I could solve this, or maybe a simpler technique to dynamically define if a field is required or not ? 您知道为什么以及如何解决这个问题,或者知道一种更简单的技术来动态定义是否需要字段吗?

Thanks in advance ! 提前致谢 !

I finally found a solution to my problem, so here is it: 我终于找到了解决我的问题的方法,就是这样:

  • I add the FormType of the embedded form as required false 我根据需要添加嵌入表单的FormType false
  • I add a PRE_SUBMIT that check if the user wants to add a vote 我添加一个PRE_SUBMIT来检查用户是否要添加投票
  • If not I delete the data (the question) that is sent to my controller 如果不是,则删除发送到控制器的数据 (问题)

In fact it was the Question that created a problem because it is OneToOne associated with my subject, which means I can't add the option allow_delete (as I do for the answers). 实际上,正是这个问题引起了问题,因为它是与我的主题相关联的OneToOne,这意味着我无法添加选项allow_delete(就像我为答案所做的那样)。
Here was my error: 这是我的错误:

A new entity was found through the relationship '...' that was not configured to cascade persist operations for entity: ... To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). 通过关系'...'找到了一个新实体,该关系未配置为级联实体的持久化操作:...解决此问题:在此未知实体上显式调用EntityManager#persist()或配置级联持久化此关联在映射中,例如@ManyToOne(..,cascade = {“ persist”})。 If you cannot find out which entity causes the problem implement '...' to get a clue. 如果您无法找出导致问题的实体,请实施“ ...”以获取线索。

For those who would like to see how i add the event here is a simple code ( highly inspired from the doc ): 对于那些想看看我如何在这里添加事件的人来说,这是一个简单的代码( 受doc的启发 ):

In the buildForm method : buildForm方法中

->addEventListener(FormEvents::PRE_SUBMIT, array($this, 'onPreSubmit'))    

Then you create another method : 然后创建另一个方法

public function onPreSubmit(FormEvent $event)
{
    $data = $event->getData();
    $form = $event->getForm();
    if($data['vote'])
    {
    }
    else
    {
        unset($data['question']);
        $event->setData($data);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM