Php Symfony2表单表单类型本身集合
我正在尝试创建一个symfony2表单“PersonType”,它有一个Php Symfony2表单表单类型本身集合,php,forms,symfony,Php,Forms,Symfony,我正在尝试创建一个symfony2表单“PersonType”,它有一个PersonType集合字段,应该映射一个人的孩子 我得到了这个错误 {"message":"unable to save order","code":400,"errors":["This form should not contain extra fields."]} 这是我的人实体 class Person { private $id; /** * @ORM\OneToMany(targe
PersonType
集合字段,应该映射一个人的孩子
我得到了这个错误
{"message":"unable to save order","code":400,"errors":["This form should not contain extra fields."]}
这是我的人
实体
class Person
{
private $id;
/**
* @ORM\OneToMany(targetEntity="Person", mappedBy="parent", cascade={"persist"})
*/
private $children;
/**
* @ORM\ManyToOne(targetEntity="Person", inversedBy="children")
* @ORM\JoinColumn(name="orderitem_id", referencedColumnName="id", nullable=true)
*/
private $parent;
}
还有我的类型
class PersonType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('id')
->add('children', 'collection', array(
'type' => new PersonType()
))
;
}
更新:
我发现问题是因为选项:
'allow_add' => true,
'by_reference' => false
不在该类型中,我已将其删除,因为当我插入它们时,表单不会出现,页面也不会出现错误
我很困惑,因为有了这个错误,人们就不能生孩子了:/
有没有人已经面临同样的问题?(嵌套在自身上的formType)
实际:
我已将我的personType复制到PersonchildrenType,以便在第一个中插入最后一个…尝试将您的表单注册为服务,如下所述:,并修改您的表单,如下所示:
class PersonType extends AbstractType
{
public function getName()
{
return 'person_form';
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('id')
->add('children', 'collection', array(
'type' => 'person_form',
'allow_add' => true,
'by_reference' => false
))
;
}
}
我遇到了相同的问题,只是错误消息是: FatalErrorException:错误:已达到“MAX”的最大函数嵌套级别,正在中止 这是正常的,因为“PersonType”试图用一个新的“PersonType”字段构建表单,而“PersonType”字段也试图用一个新的“PersonType”字段构建表单,以此类推 因此,目前我解决这个问题的唯一方法是分两步进行:
public function addAction(Person $parent=null){
$person = new Person();
$person->setParent($parent);
$request = $this->getRequest();
$form = $this->createForm(new PersonType(), $person);
if($this->getRequest()->getMethod() == 'POST'){
$form->bind($request);
if ($form->isValid()) {
// some code here
return $this->redirect($this->generateUrl('path_to_person_add', array(
'id' => $person->getId()
); //this redirect allows you to directly add a child to the new created person
}
}
//some code here
return $this->render('YourBundle::yourform.html.twig', array(
'form' => $form->createView()
));
}
我希望这能帮助你解决你的问题。
如果你不明白或者我完全错了,告诉我;) (感谢编辑,我的英语很差^^)我会为初学者嵌入另一个表单,而不是同一个表单本身。看看是否有效。一对另一:效果很好,我读过医生:但是,关于这个案子什么都没有。