2013-05-03 59 views
0

我想要做什麼: 我試圖從集合中的嵌入式formType訪問字段。 我可以通過$form->get('childType')輕鬆訪問第一級(因此獲得集合),但我很難訪問嵌入在childType中的字段。 我試過$form->get('childType')->get('anotherAttr')沒有成功。恕我直言,問題來自於一個集合不僅僅是一個字段的事實,如果沒有Symfony知道我想要做什麼的集合的項目,get('anotherAttr')不能完成。無論如何,經過很多搜索,我還沒有找到如何告訴他我想要收藏的第一件物品。以表單形式訪問collectionType中的嵌入字段

下面是代碼:

父類類型:

<?php 

namespace my\myBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilder; 

class ParentType extends AbstractType 
{ 
    public function buildForm(FormBuilder $builder, array $options) 
    { 
     $builder->add('attribute1','text',array("label" =>  'attribute 1 :')) 
       ->add('childType','collection',array('type' => new ChildType($options['attrForChild']))); 
    } 

    public function getDefaultOptions(array $options) 
    { 
     return array(
      'data_class' => 'my\myBundle\Entity\Parent', 
      'attrForChild'   => null 
     ); 
    } 

    public function getName() 
    { 
     return 'my_mybundle_childtype'; 
    } 
} 

的childClassType:

<?php 

namespace my\myBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilder; 

    class ChildType extends AbstractType 
    { 
     private $childAttr; 

    public function __construct($childAttr=null){ 
     $this->childAttr=$childAttr; 
    } 

    public function buildForm(FormBuilder $builder, array $options) 
    { 
     $builder->add('childAttr','text',array("label" => 'childAttr : ','property_path' => false)); 
       if(isset($this->childAttr)){ 
        $childAttr = $this->childAttr; 
        $builder->add('childAttrDependantEntity','entity',array("label" => 'RandomStuff : ', 
         'class' => 'mymyBundle:randomEntity', 
         'property' => 'randProperty', 
         'multiple' => false, 
         'query_builder' => function(\my\myBundle\Entity\randomEntityRepository $r) use ($childAttr) { 
          return $r->findByChildAttr($childAttr); 
         } 
        )); 
       } 
       $builder->add('anotherAttr','text',array("label" => 'Other attr : ')) 
    } 

    public function getDefaultOptions(array $options) 
    { 
     return array(
      'data_class' => 'crri\suapsBundle\Entity\Adresse', 
      'childAttr'   => null 
     ); 
    } 

    public function getName() 
    { 
     return 'my_mybundle_childtype'; 
    } 
} 

此外,沒有我使用childAttr解決方案是確定的? (它正在工作,但感覺有點像黑客,是否有更乾淨的方式來做同樣的事情?)。它用於什麼=用戶給我一個文本字段,我驗證它是否存在於數據庫中,如果它存在,我將entityType添加到與此屬性相關的表單中。目標是用戶將從限制列表中選擇元素,而不是從數據庫中的所有元素。

編輯:控制器的相應代碼:

public function parentTypeAddAction(Request $request){ 
    $parentEntity = new ParentEntity(); 
    $parentEntity->addChildEntity(new ChildEntity()); 
    $form = $this->createForm(new ParentType,$parentEntity); 
    if ($request->getMethod() == 'POST') { 
     $form->bindRequest($request); 
     // Testing (everything I tried) 
     $test=$form->get('childType')->getAttribute('childAttr'); 
     /** 
     $test=$form['childAttr']; 
     $test=$form->get('childAttr'); **/ 
     return $this->container->get('templating')->renderResponse('myMyBundle:Default:test.html.twig', 
       array('test' => $test)); 
     if($test!=null){ 
       $anEntity = $em->getRepository('crrisuapsBundle:AnEntity')->find($test); 
       if($anEntity==null){ 
        $form->get('childType')->get('childAttr')->addError(new FormError("Invalid attribute.")); 
       } else { 
        $form = $this->createForm(new ParentType,$parentType,array('childAttr' => $test)); 
        $individu->getAdresses()->first()->setAnEntity($anEntity); 
       } 
      } 
     $form->bindRequest($request); 
     if($request->request->get('CHILDATTRPOST')!='Search attribute'){ 
      if ($form->isValid()) { 
       $em->persist($parentType); 
       $em->persist($individu->getChildEntity()->first()); 
       $em->flush(); 
       return $this->redirect($this->generateUrl('myMyBundle_homepage'), 301); 
      } 
     } 
    } 
    return $this->container->get('templating')->renderResponse('myMyBundle:Default:parentTypeAdd.html.twig', 
      array('form' => $form->createView())); 
} 
+0

你能告訴我們控制器代碼嗎? – cheesemacfly 2013-05-03 16:12:45

+0

將控制器的代碼添加到問題的正文中。 – MisterJ 2013-05-06 06:56:23

+0

如果你的數據發佈時'var_dump($ form-> get('childType'))'怎麼辦?我不明白當你使用'$ individu-> addChildType(new ChildType());':對象'$ individualidu'從哪裏來?當你創建'form'時,你給它'新的ParentType'和第二個參數'$ parentType',這是同樣的事情。您應該傳遞一個'my \ myBundle \ Entity \ Parent'對象。它是'parentTypeAddAction()'的完整代碼嗎? – cheesemacfly 2013-05-06 15:52:00

回答

1

由於cheesemacfly的建議我能想出如何得到它。這裏是解決方案:

//Getting the childEntities forms as an array 
$childArray=$form->get('childType')->getChildren(); 
//Getting the childEntity form you want 
$firstChild=$childArray[0]; 
//Getting your attribute like any form 
$childAttrForm=$childArray[0]->get('childAttr'); 
$childAttr=$childAttrForm->getData(); 
+1

請注意,如果您使用PHP> 5.4,則可以重寫'$ childAttr = $ form-> get('childType') - > getChildren()[0] - > get('childAttr');'。請參見[數組解除引用]一節(http://docs.php.net/manual/en/language.types.array.php)。 – cheesemacfly 2013-05-07 14:30:14

+0

這很好,不知道。感謝所有@cheesemacfly。 – MisterJ 2013-05-07 14:39:40

相關問題