dream3323 2015-07-30 15:36
浏览 24

Symfony Form坚持奇怪的组合?

I am currently facing a problem, I have a symfony form with twice the same entity inside. Two diplomas. For one of theme i have to modify one field and for the other 3 fields. Everything works fine until i try to persist the data's then symfony just melt in pure agony and do a nonsense thing persist stuff. It mix the one field of my first entity with the three other fields of my second entity.

Basicaly my builder :

if($options['diplomas_only']){
        $builder->add('acquired_diploma', new DiplomaType(), array('viewer_role' => $options['viewer_role']))
                ->add('prepared_diploma', new DiplomaType(), array('viewer_role' => $options['viewer_role'], 'prepared' => true))
                ->add('save', 'submit');
    }

my diploma type :

$builder
        ->add('level', new DiplomaLevelType(), array('disabled' => false))
        ->add('category', new DiplomaCategoryType(), array('disabled' => false))
        ->add('title', new DiplomaTitleType(), array('disabled' => false))
        ->add('blueorwhite', 'text', array('disabled' => true));

My Controller :

 /**
 * @Route("/{_locale}/cursus/{contractId}", requirements={"_locale" = "fr|en"}, defaults={"_locale" = "fr"}, name="poljeunefront_cursus")
 */
public function cursusAction ($locale = 'fr', Request $request, $contractId) {
    //get contract
    $em = $this->getDoctrine()->getManager();
    $contract = $em->getRepository('PolJeuneAdminBundle:Generic_Contract')->findOneById($contractId);
    $viewer = $this->get('security.context')->getToken()->getUser();
    $formGenericContract = $this->get('form.factory')
        ->createBuilder(new Generic_ContractType(), $contract, array('diplomas_only' => true, 'viewer_role' => $viewer->getCurrentRole()))
        ->getForm()
        ->handleRequest($request);
    if ($viewer->isBeneficiary()) {
        if ($formGenericContract->get('save')->isClicked()) {
            if ($formGenericContract->isValid()) {
                var_dump($formGenericContract->get('acquired_diploma'));
                exit();
                $em = $this->getDoctrine()->getManager();
                $em->persist($contract);
                $em->flush();
                $savingStatus = true;
            }
            else {
                $savingStatus = false;
            }
        }
    }

    return $this->render('PolJeuneFrontBundle:Contract:cursus.html.twig', array(
        'formGenericContract' => $formGenericContract->createView(),
    ));
}

and finaly my twig :

{% set formAttributes = readonly is defined and readonly == true ? {} : { 'data-parsley-validate': '', 'data-parsley-excluded': '[disabled]' } %}
    {% form_theme formGenericContract "PolJeuneFrontBundle:Form:form_theme.html.twig" %}
    {{ form_start(formGenericContract, {
        'attr': formAttributes
    }) }}
    <h3>{{ 'cursus.title'|trans({}, 'contract') }}</h3>
    <h4>{{ 'prepared.title'|trans({}, 'contract') }}</h4>
    <div class="row">
        <div class="col-sm-3">
            {{ form_row(formGenericContract.prepared_diploma.level.level) }}
        </div>
        <div class="col-sm-3">
            {{ form_row(formGenericContract.prepared_diploma.category.category) }}
        </div>
        <div class="col-sm-3">
            {{ form_row(formGenericContract.prepared_diploma.title.title) }}
        </div>
        <div class="col-sm-3">
            {{ form_row(formGenericContract.prepared_diploma.blueorwhite) }}
        </div>
    </div>
    <hr>
    <h4>{{ 'acquired.title'|trans({}, 'contract') }}</h4>
    <div class="row">
        <div class="col-sm-3">
            {{ form_row(formGenericContract.acquired_diploma.level.level) }}
        </div>
        <div class="col-sm-3">
            {{ form_row(formGenericContract.acquired_diploma.category.category) }}
        </div>
        <div class="col-sm-3">
            {{ form_row(formGenericContract.acquired_diploma.title.title) }}
        </div>
        <div class="col-sm-3">
            {{ form_row(formGenericContract.acquired_diploma.blueorwhite) }}
        </div>
    </div>
    {{ form_row(formGenericContract.save) }}
    {{ form_end(formGenericContract) }}
    <hr>

Thank's for reading

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么