【发布时间】:2011-12-01 00:31:06
【问题描述】:
我想创建一个可以添加多个 etape 的表单。我创建这样的表单:
//形式
namespace RBO\TryBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use RBO\TryBundle\Entity\Try;
class TryType extends AbstractType {
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('etapes', 'collection', array(
'type' => new EtapeType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'label' => 'Etapes'
));
}
public function getName()
{
return 'try';
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'RBO\TryBundle\Entity\Try',
'csrf_protection' => true,
'csrf_field_name' => '_token',
// a unique key to help generate the secret token
'intention' => 'try_item',
);
}
}
// EtapeType
<?php
namespace RBO\TryBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use RBO\TryBundle\Entity\Etape;
class EtapeType extends AbstractType {
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('name', 'text');
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'RBO\TryBundle\Entity\Etape',
);
}
public function getName()
{
return 'etape';
}
}
// 在树枝模板中显示
{{ form_row(form.etapes) }}
实体 Try 有一个属性 etapes,它是一个 ArrayCollection(在构造函数中定义)
此代码不会渲染任何期望的标签。我错过了什么吗?
提前谢谢你
【问题讨论】:
标签: forms collections symfony