2016-11-07 97 views
1

從Symfony開始是一個相當的學習曲線。即使在閱讀了幾個小時之後,我也無法理解這個大概簡單的問題。我想從一個實體中加載一個選擇形式的值。symfony:訪問控制器變量形式

控制器:

namespace AppBundle\Controller 
class ItemController extends Controller 
{ 
    public function itemAction (Request $request) 
    { 
     $myItems = new Itemlist(); 
     //some statements to fill $myItems 
     $form = $this->createForm (AllitemsType::class, $myItems); 
     // some more stuff 
     return $this->render (...); 
    } 
} 

實體:

namespace AppBundle\Entity; 
class Itemlist 
{ 
    protected $choices; 
    protected $defaultvalue; 

    public function __construct() 
    { 
    $choices = array(); 
    } 

    // all the get and set-methods to fill/read the $choices array and $defaultvalue 
} 

形式:

namespace AppBundle\Form 
class AllitemsType extends AbstractType 
{ 
    public function buildForm (FormBuilderInterface $builder, array $options) 
    { 
    // and here is my problem: how can I fill next two lines with values from the Itemlist-Entity? 
    // The Itemlist instance has been build in the controller and is unknown here 
    $items = ??? // should be 'AppBundle\Entity\Itemlist->$choices 
    $defaultitem = ??? // should be 'AppBundle\Entity\Itemlist->$defaultvalue 

    $choices_of_items = array (
     'choices' => $items, 
     'expanded' => true, 
     'multiple' => false, 
     'data' => $defaultitem, 
    ); 

    $builder->add ('radio1', ChoiceType::class, $choices_of_items); 

    } 
} 

任何幫助表示讚賞, 鎢

回答

2
$builder->add('choices', ChoiceType::class); 

應該是足夠的,因爲您將一個實體綁定到表單,獲取值並將其設置回來的過程是自動的。當然,你需要有setter和getter爲choicesAllitemsType

給一個完整的答案 - 上面的部分是所謂的「最佳實踐一個」 - 你還可以選擇以下

$items = $options['data']; 
之一

$builder->addEventListener(
    FormEvents::PRE_SET_DATA, function (FormEvent $event) { 
    $allItems = $event->getData(); 
    $form = $event->getForm(); 
    $form->add('radio1', ChoiceType::class, [ 
     'choices' => $allItems 
    ]); 
}); 

第二個應該被優選作爲在options['data'],實體可以形式事件的生命週期期間改變。

+0

您的「最佳實踐」答案確實解決了填充選項,但並不尊重填充「數據」屬性,對嗎? 對不起,我不抓住其他兩個可選答案。 無論如何感謝您的貢獻。 – Wolfram

+0

@wolfram對不起,我忘記了數據選項,但是我用於選擇的方法相同 – DonCallisto

0

傳遞變量與createForm對象。

控制器:

namespace AppBundle\Controller 
    class ItemController extends Controller 
    { 
     public function itemAction (Request $request) 
     { 
      $myItems = new Itemlist(); 
      $formVars = array("items" => array(1,2,3,4,6), "defaultItems" => 2); // Store variables 
      ^^ 

      //some statements to fill $myItems 
      $form = $this->createForm (new AllitemsType($formVars), $myItems); 
                 ^^ 
      // some more stuff 
      return $this->render (...); 
     } 
    } 

現在form創建窗體的構造,並設置類變量itemsdefaultitem

形式:

namespace AppBundle\Form 
class AllitemsType extends AbstractType 
{ 

    $this->items = array(); 
    $this->defaultitem = 0; 

    public function __construct($itemArr) 
    { 
    $this->items = $itemArr['items']; 
    $this->defaultitem = $itemArr['defaultItems']; 
    } 

    public function buildForm (FormBuilderInterface $builder, array $options) 
    { 
    $choices_of_items = array (
     'choices' => $this->items, // User class variable 
     'expanded' => true, 
     'multiple' => false, 
     'data' => $this->defaultitem, // User class variable 
    ); 

    $builder->add ('radio1', ChoiceType::class, $choices_of_items); 

    } 
} 

應該解決您的問題。

+1

非常感謝。這正是我所尋找的。 – Wolfram

+0

不要這樣做。新的FormType()已在Symfony 3.x中刪除。通過options參數傳遞附加信息。 – Cerad

+1

@Cerad看起來像Wolfram根據他提供的代碼使用Symfony 2.x版本 – Janvi