doctrine-orm - Symfony2 : customize form labels in form collections

标签 doctrine-orm symfony

我正在尝试自定义在子表单中生成的表单标签。

我想显示特定比赛周中包含的足球赛程,如下所示:

- Fixture 1 : Manchester United (0) - (1) Arsenal
- Fixture 2 : Chelsea (2) - (1) Liverpool
- ...

My form displays all fixtures and related scores but all labels contain the database column names (score1, score2). I want to put team names instead. So, it currently shows:

- Fixture 1 : score1 (0) - (1) score2
- Fixture 2 : score1 (2) - (1) score2
- ...

In the controller, I generate the week form (WeekType). $week contains week data and fixtures data using $week->getFixtures().

Controller/DefaultController.php

$form = $this->createForm(new WeekType(), $week)->createView();

return array(
    'form' => $form,
);

表单/WeekType.php

class WeekType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('fixtures', 'collection', array(
            'type' => new FixtureType(),
        ));
    }
 }

Fixture 表单添加了 2 个字段。我想将默认标签替换为团队名称。
但是我无法以这种形式访问夹具数据。 $options 为 NULL。我以为 $options['data'] 会包含灯具数据......但我错了。

表单/FixtureType.php

class FixtureType extends AbstractType
{  
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('score1', 'text', array('label' => **WHAT TO PUT HERE**));
        $builder->add('score2', 'text', array('label' => **WHAT TO PUT HERE**));
    }
}

我使用此代码显示所有灯具,并且效果很好。

index.html.twig
    {% for fixture in week.form.fixtures %}
        {{ form_widget(fixture) }}
    {% endfor %}

也许我可以直接在 index.html.twig 中自定义我的标签,但我怎样才能获得装置数据?

有人遇到这个问题并解决了吗?

最佳答案

我找到了解决办法!

在“index.html.twig”模板中,我迭代了表单元素。
那是一个错误。我只需要遍历设备并获取相关的表单小部件。

index.html.twig

{% for fixture in week.fixtures %}
    fixture.HomeTeam.name
    {{ form_widget(week.form.fixtures[loop.index0]) }}
    fixture.AwayTeam.name
{% endfor %}

诀窍是直接从表单小部件数组中检索表单元素:
    week.form.fixtures[loop.index0]

关于doctrine-orm - Symfony2 : customize form labels in form collections,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6726878/

相关文章:

php - Doctrine ORM 和具有抽象类策略的工厂

Symfony 通行证数据可查看

mysql - Symfony - 选择日期 = 今天的对象

php - DQL 子查询问题

symfony - 多对多、一对多或多对一

symfony2 缓存清除 --env=prod 不打印任何内容

php - symfony doctrine 将一个实体对象移动到另一个实体类型

php - COUNT = 0 的 Doctrine2 DQL 问题

php - postUpdate 和 postPersist 在学说上的区别

Symfony 形式多对一一对多