【问题标题】:how to set default selected value in a liste with entity type field如何在具有实体类型字段的列表中设置默认选定值
【发布时间】:2016-03-11 15:50:39
【问题描述】:

这是我在控制器中的代码:

$entity_options = array(
    'class' => 'ACCUEILBundle:Param',
    'property' => 'description',
    'query_builder' => function(\Doctrine\ORM\EntityRepository $er) {
        return $er->createQueryBuilder('p')
                ->where("p.categorie = 5")
                ->orderBy('p.description', 'ASC');
    }
);

if($entity_type !== null) {
    $entity_options['data'] = $this->getDoctrine()->getManager()->find('ACCUEILBundle:Param', $entity_type);
}

$formBuilder->add('entity_types', 'entity', $entity_options);

$form->$formBuilder->getForm();

如果我在列表中选择一个项目并提交表单,则 $form->getData()['entity_types'] 返回所选的 Param 对象。

如果我想用'data'选项强制默认选中的项目并显示$form->getData(),它会返回一个空数组。

我应该从哪里获得我刚刚使用“数据”选项声明的默认选定项。

【问题讨论】:

    标签: forms symfony


    【解决方案1】:

    您可以在创建表单时传递价值。

    $formBuilder = $this->createFormBuilder(['entity_types' => $initialValueForEntityTypeField])
    

    【讨论】:

    • 什么是 $initialValueForEntityTypeField ?一个参数对象?
    • 是的,已分配给此类型的实体。
    猜你喜欢
    • 2013-06-24
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    • 2012-11-20
    • 2020-08-12
    • 2019-06-03
    • 2013-11-01
    • 2016-01-06
    相关资源
    最近更新 更多