Forms Symfony2在表单集合中更新特定字段

Forms Symfony2在表单集合中更新特定字段,forms,symfony,collections,Forms,Symfony,Collections,我有一个有趣的问题。我在stackoverflow和google中找不到任何解决方案。我有一个实体User和User有一些元。所以我创建了一个UserMeta实体和UserMetaValue。在用户表单中有许多选项卡。我在选项卡中使用了这些元。有些在第一个选项卡中,有些在其他选项卡中。所有选项卡都有自己的形式。当我在活动选项卡上绑定表单时,活动选项卡元将更新,而其他选项卡元将更改为NULL StudentPersonalType.php namespace ATL\UserBundle\Form

我有一个有趣的问题。我在stackoverflow和google中找不到任何解决方案。我有一个实体
User
User
有一些元。所以我创建了一个
UserMeta
实体和
UserMetaValue
。在用户表单中有许多选项卡。我在选项卡中使用了这些元。有些在第一个选项卡中,有些在其他选项卡中。所有选项卡都有自己的形式。当我在活动选项卡上绑定表单时,活动选项卡元将更新,而其他选项卡元将更改为NULL

StudentPersonalType.php

namespace ATL\UserBundle\Form\Type;

use ATL\CommonBundle\Utils\Shortcut;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormBuilderInterface;

class StudentPersonalType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options){
        $builder->add("first_name", null, array(
                "label" => "İsim",
                "required" => true,
                "attr" => array(
                    "class" => "span10"
                )
            ))->add("last_name", null, array(
                "label" => "Soyisim",
                "required" => true,
                "attr" => array(
                    "class" => "span10"
                )
            ))->add("username", null, array(
                "label" => "Öğrenci Numarası",
                "required" => true,
                "attr" => array(
                    "class" => "span10"
                )
            ))->add("email", null, array(
                "label" => "Email",
                "required" => true,
                "attr" => array(
                    "class" => "span10"
                )
            ))->add('metas', 'collection', array(
                'label' => "Metas",
                'type' => new UserMetaType()
            ));
    }

    public function getName(){
        return "personal";
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver){
        $resolver->setDefaults(array(
            "data_class" => "ATL\UserBundle\Entity\User"
        ));
    }
}
namespace ATL\UserBundle\Form\Type;

use ATL\CommonBundle\Utils\Shortcut;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormBuilderInterface;

class StudentEducationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options){
        $builder->add('metas', 'collection', array(
                'label' => "Metas",
                'type' => new UserMetaType(),
                'by_reference' => false
            ));
    }

    public function getName(){
        return "education";
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver){
        $resolver->setDefaults(array(
            "data_class" => "ATL\UserBundle\Entity\User"
        ));
    }
}
StudentEducationType.php

namespace ATL\UserBundle\Form\Type;

use ATL\CommonBundle\Utils\Shortcut;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormBuilderInterface;

class StudentPersonalType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options){
        $builder->add("first_name", null, array(
                "label" => "İsim",
                "required" => true,
                "attr" => array(
                    "class" => "span10"
                )
            ))->add("last_name", null, array(
                "label" => "Soyisim",
                "required" => true,
                "attr" => array(
                    "class" => "span10"
                )
            ))->add("username", null, array(
                "label" => "Öğrenci Numarası",
                "required" => true,
                "attr" => array(
                    "class" => "span10"
                )
            ))->add("email", null, array(
                "label" => "Email",
                "required" => true,
                "attr" => array(
                    "class" => "span10"
                )
            ))->add('metas', 'collection', array(
                'label' => "Metas",
                'type' => new UserMetaType()
            ));
    }

    public function getName(){
        return "personal";
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver){
        $resolver->setDefaults(array(
            "data_class" => "ATL\UserBundle\Entity\User"
        ));
    }
}
namespace ATL\UserBundle\Form\Type;

use ATL\CommonBundle\Utils\Shortcut;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormBuilderInterface;

class StudentEducationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options){
        $builder->add('metas', 'collection', array(
                'label' => "Metas",
                'type' => new UserMetaType(),
                'by_reference' => false
            ));
    }

    public function getName(){
        return "education";
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver){
        $resolver->setDefaults(array(
            "data_class" => "ATL\UserBundle\Entity\User"
        ));
    }
}
和树枝

    <div id="personal-info" class="tab-pane row-fluid active">
       <form style="margin:20px 0 0 0;" class="ajaxForm form-horizontal form-row-seperated" action="{{ formAction }}">
            {{ form_row(form.first_name) }}
            {{ form_row(form.last_name) }}
            {{ form_row(form.email) }}
            {{ form_row(form.username) }}
            {% for meta in form.metas %}
                {% if meta.value.vars.label in formValues.personal %}
                    {{ form_widget(meta) }}
                {% endif %}
            {% endfor %}
            {{ form_row(form._token) }}
            <div class="form-actions" style="margin-bottom:0;">
                <button class="btn blue" type="submit"><i class="icon-ok"></i> Kaydet</button>
            </div>
        </form>
    </div>
    <div id="education-info" class="tab-pane row-fluid">
        <form style="margin:20px 0 0 0;" class="ajaxForm form-horizontal form-row-seperated" action="{{ formAction }}">
            {% for meta in educationForm.metas %}
                {% if meta.value.vars.label in formValues.education %}
                    {{ form_widget(meta) }}
                {% endif %}
            {% endfor %}
            {{ form_row(educationForm._token) }}
            <div class="form-actions" style="margin-bottom:0;">
                <button class="btn blue" type="submit"><i class="icon-ok"></i> Kaydet</button>
            </div>
        </form>
    </div>

{{表格行(表格名)}
{{form_row(form.last_name)}
{{form_row(form.email)}
{{form_行(form.username)}
{%form.metas%}
{%if-meta.value.vars.label在formValues.personal%}
{{form_widget(meta)}
{%endif%}
{%endfor%}
{{form_row(form._token)}
凯代特
{educationForm.metas%中的meta为%s}
{%if formValues.education%中的meta.value.vars.label}
{{form_widget(meta)}
{%endif%}
{%endfor%}
{{form_行(educationForm.\u令牌)}
凯代特
我通过使用IF语句在twig文件中检查集合字段来过滤集合字段


我又问了一个问题,如何在同一页面中使用不同形式的meta而不影响其他页面?

过滤掉不相关字段以进行渲染的工作已经完成了一半。但是您还需要过滤掉表单绑定的无关字段

当您将请求绑定到表单时,它期望每个
UserMeta
实体都有值,因为在
UserMetaType
集合中有一个字段用于所有实体。您需要删除所有与您提交的值之一不对应的
UserMetaType
表单。最好使用
FormEvents::PRE_BIND
侦听器


您可以在中看到一个更简单的示例。这对您来说会稍微复杂一点,因为您必须遍历
UserMetaType
表单的集合,并删除您不希望绑定的表单。

您已经完成了筛选出不相关字段以进行渲染的一半工作。但是您还需要过滤掉表单绑定的无关字段

当您将请求绑定到表单时,它期望每个
UserMeta
实体都有值,因为在
UserMetaType
集合中有一个字段用于所有实体。您需要删除所有与您提交的值之一不对应的
UserMetaType
表单。最好使用
FormEvents::PRE_BIND
侦听器

您可以在中看到一个更简单的示例。它对您来说会稍微复杂一些,因为您必须遍历
UserMetaType
表单的集合,并删除不希望绑定的表单