【问题标题】:Display a a related entity property value as a form label in symfony在 symfony 中将相关实体属性值显示为表单标签
【发布时间】:2013-05-02 11:14:30
【问题描述】:

这源于这个问题,但我的问题略有改变:Odd many-to-many form rendering with symfony and doctrine

我的实体是 Formula 一对多与 FormulaColor 多对一与 Color。

公式(ID、代码、名称) FormulaColor(formula_id、color_id、百分比) 颜色(id、代码、名称)

一个公式可以有一种或多种颜色,每种颜色占该公式的百分比。

我正在尝试创建一个公式编辑类型,它将显示给定公式的百分比字段以及每个百分比字段的标签或标题,即颜色->标签名称。我已经显示了公式的百分比字段,但我想用颜色名称标记每个字段。我怎样才能做到这一点?我是否必须以某种方式使用查询构建器?

我有一个如下所示的 FormulaAddEditType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('code', null, array(
                'label' => 'Code'
            ))
        ->add('name', null, array(
                'label' => 'Name'
            ));

    $builder->add('formulaColors', 'collection', array(
            'type' => new FormulaColorType(),
            'allow_add' => true,
            'allow_delete' => true,
            'prototype' => true,
        ));
}

然后是 FormulaColorType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('percentage', 'number', array(
            'label' => new ColorAddEditType()
        ));
}

ColorAddEditType

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('code', null, array(
                'label' => 'Code'
            ))
        ->add('name', null, array(
                'label' => 'Name'
            ))
    ;
}

控制器动作

/**
 * @Route("/formulas/{id}/edit")
 * @Method({"GET", "POST"})
 * @Template()
 */
public function editAction(Request $request, $id)
{
    $em = $this->getDoctrine()->getManager();

    $formula = $em->getRepository('PrismPortalCommonBundle:Formula')->find($id);

    if (!$formula) {
        throw $this->createNotFoundException('Unable to find Formula entity.');
    }

    $form = $this->createForm(new FormulaAddEditType(), $formula);

    if ($request->isMethod('POST')) {
        $form->bind($request);

        if ($form->isValid()) {

            $em->persist($formula);
            $em->flush();

            return $this->redirect($this->generateUrl('prism_portal_admin_dashboard_index'));
        }
    }

    return array(
        'formula' => $formula,
        'form' => $form->createView()
    );
}

我能够在表单事件订阅者中获得我想要的结果。订阅者如下所示:

class AddPercentFieldSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        // Tells the dispatcher that you want to listen on the form.pre_set_data
        // event and that the preSetData method should be called.
        return array(FormEvents::PRE_SET_DATA => 'preSetData');
    }

    public function preSetData(FormEvent $event)
    {
        $data = $event->getData();
        $form = $event->getForm();

        // If it's not a new Formula, then I want to show the percentage fields.
        if ($data) {
            $form->add('percentage', 'text', array(
                    'label' => $data->getColor()->getCode(),
                ));
        }
    }
}

【问题讨论】:

  • 请在您创建此表单的位置发布控制器代码。
  • @Lighthart 我已经从控制器中添加了动作。
  • 这些标签仅适用于现有公式?

标签: symfony


【解决方案1】:

我对您的实体的外观做了一些猜测,但我认为这大致是您想要的,例如:

公式添加编辑类型

    public function buildForm(FormBuilderInterface $builder, array $options) {

    $entity=$builder->getData();
    $colors=$entity->getColors());

    $builder
    ->add('code', null, array(
        'label' => 'Code'
        ))
    ->add('name', null, array(
        'label' => 'Name'
        ));

    $colors->map(function ($color) use ($builder) {
        $builder->add($color->getName()
            , null, 
            array(
                'label' => $color->getName()
                )
            )
    });
}

【讨论】:

  • 感谢 lighthart,实际上我只是能够得到一些看起来与您所做的非常相似的东西,除了我使用表单事件订阅者(请参阅我编辑的帖子以了解我所做的) .你认为一种方法比另一种更好吗?
  • 我没有订阅用户的经验。如果它有效并且你对它很满意,那么这是最好的。请将您的方法作为您自己的答案发布,并接受该答案,这样其他人就不会为了寻找其他解决方案而费尽心思。
  • $colors=$entity.getColors()); 什么?
  • 哎呀。在那里转了一分钟的另一种语言。固定。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-08
  • 2010-11-05
  • 2022-11-12
  • 1970-01-01
  • 2015-11-28
  • 1970-01-01
相关资源
最近更新 更多