2013-10-30 3 views
0

내가 심포니 2 형태 'PersonType', 주어진 사람의 아이들을지도해야 PersonType 수집 필드가 만들려고 해요.Symfony2 양식 FormType 자체 수집

그리고이 오류를 받고 있어요,

여기
{"message":"unable to save order","code":400,"errors":["This form should not contain extra fields."]} 

Person 실체이며,

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() 
      )) 
     ; 
    } 

는 UPDATE : 나는 보았다 문제는 옵션 때문이었다 :

'allow_add' => true, 
'by_reference' => false 

은 유형이 아니므로 삽입 할 때 양식이 나타나지 않고 페이지가 오류없이 중단되므로 삭제했습니다./

누군가가 이미 같은 문제에 직면 하는가 :

내가이 오류로 인해 매우 혼란 스러워요, 사람들은 아이를 가질 수없는 이유는 무엇입니까? (자체 이상 중첩 된 formType) ACUTALLY

: 여기에 설명 된 것처럼 내가 처음에이 마지막을 삽입 PersonchildrenType 내 personType을 복제했습니다 ...

+0

(감사 편집을 위해, 나는 우선 또 다른 양식을 삽입 할 – pbenard

+0

) ^^ 영어 매우 가난 해요,하지 동일 하나, 내가 completly 틀렸다면 당신이 뭔가를 이해하지 못하는 경우에 저에게 또는 그 자체로. 그리고 그것이 효과가 있는지보십시오. 다른 –

+0

한이 사건에 대해 http://symfony.com/doc/current/cookbook/form/form_collections.html하지만, 아무것도 : 그것은 완벽하게 작동, 나는 문서를 읽었습니다. – pbenard

답변

0

봅니다 서비스로 양식을 등록 : http://symfony.com/doc/current/book/forms.html#defining-your-forms-as-services를 ,이 같은 양식을 수정

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 
      )) 
     ; 
    } 
} 
+0

고마워, 나는 노력하고있어, 여전히 같은 결과를 ... – pbenard

1

내가 제외하고는 동일한 문제가 된 오류 메시지가했다 :

FatalErrorException: Error: Maximum function nesting level of 'MAX' reached, aborting!

"PersonType"이 새로운 "PersonType"필드가있는 양식을 작성하려고 시도하고 있기 때문에 정상적인 현상입니다 ...

그래서 유일한 방법은 나는이 문제를 해결하기 위해 관리, 순간, 두 개의 서로 다른 단계로 진행하는 것입니다

  1. 부모
  2. "링크"그 부모에 아이를 만들기 만들기

당신이 할 수있는 단순히 컨트롤러에서이 작업을 수행하십시오.

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() 
    )); 
} 

16,나는 이것이 당신이 당신의 문제를 해결하는 데 도움이 될 수 있기를 바랍니다.)