【问题标题】:Symfony - Form - preferred choice for radio buttonSymfony - 表单 - 单选按钮的首选
【发布时间】:2015-03-21 10:50:00
【问题描述】:

如何在单选按钮的表单中设置首选?

当我加载网页时,我需要检查一个特定的单选(我的 4 个单选按钮中的最后一个)...

但我读到“首选选择”仅用于选择列表,而不用于单选或复选框... http://symfony.com/doc/current/reference/forms/types/choice.html#preferred-choices

我试过了:

->add('Offre', 'entity', array(
                        'class' => 'AVCMediasBundle:Offre',
                        'query_builder' => function (EntityRepository $repository) use ($parametre_langue) {
                            return $repository->createQueryBuilder('q')
                                ->where('q.id = 4')
                                ->orderBy('q.prix', 'DESC');
                        },
                        'property' => 'titre' . $parametre_langue,
                        'expanded' => true,
                        'multiple' => false,
                        'label' => false,
                        'preferred_choices' => array('1'),
                        'attr' => array(
                            'class' => 'langue_selector'
                        )
                    ));

谢谢

【问题讨论】:

  • 你试过“数据” => '1' 吗?
  • 是的,但什么也没发生...
  • 可能是因为该字段中的数据由实体组成,但是您将preferred choices设置为字符串数组?我的意思是,你尝试过类似'preferred_choices' => array($preferredEntity), 的东西吗?
  • 来自文档的悲伤消息:“请注意,首选选项仅在呈现为选择元素时才有意义(即扩展为假)”
  • 是的,我愿意。但我找到了另一个解决方案:我直接在实体中传递了我的选择......就像这样:$preferedOffer = $em->getRepository('AVCMediasBundle:Offre')->findOneById('4'); $DetailTransaction->setOffre($preferedOffer);

标签: forms symfony radio-button


【解决方案1】:

我的解决方案是在我加载表单时直接在实体中传递我的选择

 $preferedOffer = $em->getRepository('AVCMediasBundle:Offre')->findOneById('4');
 $DetailTransaction->setOffre($preferedOffer);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多