Symfony2:在表单集合中自定义表单标签 [英] Symfony2 : customize form labels in form collections
问题描述
我想显示在特定游戏周内包含的足球装置,如下所示:
- 装置1:曼联(0) - (1)阿森纳
- 夹具2:切尔西(2) (1)利物浦
- ...
我的表单显示所有灯具和相关分数,但所有标签都包含数据库列名称(score1,score2)。我想改成球队名称。
所以,它现在显示:
- Fixture 1:score1(0) - (1)score2
- 夹具2:score1(2) - (1)score2
- ...
在控制器中,我生成一周表单(WeekType)。 $ week包含使用$ week-> getFixtures()的周数据和灯具数据。
控制器/ DefaultController.php
$ form = $ this-> createForm(new WeekType(),$ week) - > createView();
返回数组(
'form'=> $ 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个字段。我想将默认标签替换成团队名称。
但是我无法访问此表单中的fixture数据。 $ options为NULL。我认为$ options ['data']将包含灯具数据...但我错了。
Form / FixtureType.php p>
class FixtureType extends AbstractType
{
public function buildForm(FormBuilder $ builder, array $ options)
{
$ builder-> add('score1','text',array('label'=> **这是什么**));
$ builder-> add('score2','text',array('label'=> **这里是什么**));
}
}
我使用此代码显示所有灯具,它的工作原理
index.html.twig
{%for fixture.form.fixtures%}
{{form_widget(fixture)}}
{%endfor%}
也许我可以直接在index.html.twig自定义我的标签,但是如何获取灯具数据?
有人遇到这个问题并解决吗?
我找到了一个解决方案!
在index.html.twig模板中,我迭代了表单元素。
这是一个错误。我只需要遍历灯具并获取相关的窗体小部件。
index.html.twig
{%for fixture.fixtures%}
fixture.HomeTeam.name
{{form_widget(week。 form.fixtures [loop.index0])}}
fixture.AwayTeam.name
{%endfor%}
诀窍是直接从窗体小部件数组中检索表单元素:
week.form.fixtures [loop.index0]
I'm trying to customize form labels that are generated in subforms.
I want to display soccer fixtures that are contained in a specific game week, like the following:
- 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,
);
Form/WeekType.php
class WeekType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('fixtures', 'collection', array(
'type' => new FixtureType(),
));
}
}
The Fixture form adds 2 fields. I want to replace default labels into team names. However I cannot access fixture data in this form. $options is NULL. I thought $options['data'] would contain fixtures data... but I was wrong.
Form/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**));
}
}
I display all fixtures using this code, and it works great.
index.html.twig
{% for fixture in week.form.fixtures %}
{{ form_widget(fixture) }}
{% endfor %}
Maybe I could customize my labels directly in index.html.twig but how can I get fixtures data?
Does somebody encounter this issue and resolve it?
I found a solution!
In "index.html.twig" template, I iterated over form elements. It was a mistake. I just had to iterate over fixtures and get related form widget.
index.html.twig
{% for fixture in week.fixtures %}
fixture.HomeTeam.name
{{ form_widget(week.form.fixtures[loop.index0]) }}
fixture.AwayTeam.name
{% endfor %}
The trick is to retrieve form elements directly from form widgets array :
week.form.fixtures[loop.index0]
这篇关于Symfony2:在表单集合中自定义表单标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!