I'm trying to create a symfony 2 form 'PersonType', which has a PersonType
collection field that should map a given person's children.
And I'm getting this error,
{"message":"unable to save order","code":400,"errors":["This form should not contain extra fields."]}
Here is my Person
entity,
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;
}
And my Type,
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()
))
;
}
UPDATE : I've seen that the problem was because the option :
'allow_add' => true,
'by_reference' => false
wasn't in the Type, I've deleted it because when i insert them, the form don't appear and the page crash with no error.
I'm very confused because with this error, people can't have children :/
Does anyone already faced the same problem? (A formType nested over itself)
ACUTALLY : I've duplicate my personType to PersonchildrenType to insert this last in the first...
I was having the same problem except that the error message was :
FatalErrorException: Error: Maximum function nesting level of 'MAX' reached, aborting!
Which is normal because "PersonType" is trying to build a form with a new "PersonType" field that is also trying to build a form with a new "PersonType" field and so on...
So the only way I managed to solve this problem, for the moment, is to proceed in two different steps :
You can simply do this in your controller
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()
));
}
I hope this can help you to solve your problem. Tell me if you don't understand something or if I'm completly wrong ;)
Try to register your form as a service, like described here: http://symfony.com/doc/current/book/forms.html#defining-your-forms-as-services , and modify your form like this:
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
))
;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.