我使用的是Symfony 2.0.14,我想在表单模板中显示默认值。
一个FormType绑定到一个实体,当我想添加额外的字段时,我知道选项property_path=false允许添加非实体字段,对吗?
当我处于相反的情况时,我想设置一个没有表单字段的实体字段。
好的,我只需要给“createForm”一个默认实体。
但是,如何以模板形式呈现它呢?
控制器代码:
public function newAction(Request $request)
{
$game = new Game();
$local = new Role();
$visitor = new Role();
$local->setType('LOCAL');
$visitor->setType('VISITOR');
$game->addRole($local);
$game->addRole($visitor);
$form = $this->createForm(new GameType(), $game);
游戏类型代码:
public function buildForm(FormBuilder $builder, array $options){
$builder->add('teams', 'collection', array( 'type' => new RoleType()));
}
角色类型代码:
public function buildForm(FormBuilder $builder, array $options){
$builder->add('type', 'text'); // <= I would like read only for end-User
$builder->add('score', 'integer');
表单模板:
{% for role in form.teams %}
<li>
<div class="role-team">
{{ role.type }} {# WRONG way, how to do ? #}
{{ form_row(role.score) }}
</div>
</li>
{% endfor %}