2016-12-13 148 views
0

我創建了一個多選表單,但不是鍵值,而是顯示了鍵,在後端一切正常,可以顯示值而不是鍵,這是表單代碼:顯示值而不是鍵

$Themes[]=$options["data"][0]['Themes']; 
$Styles[]=$options["data"][0]['Style']; 

$builder 
    ->add("Theme",ChoiceType::class,array(
     "expanded"=>true, 
     "multiple"=>false, 
     'choices'=>$Themes, 
    )) 
    ->add("Style",ChoiceType::class,array(
     "expanded"=>true, 
     "multiple"=>false, 
     'choices'=>$Styles, 
    )) 
    ->add('save',SubmitType::class,array(
     'attr' => array('class' => 'save') 
    )); 

在小枝我只是使用開始和結束小枝命令來啓動窗體。 非常感謝。 從風格和主題轉儲 Dump from Style and Themes

我修復了一個foreach循環,但我認爲它不是最好的解決方案?

編輯:全碼

<?php 

namespace AppBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; 
use Symfony\Component\Form\Extension\Core\Type\RadioType; 
use Symfony\Component\Form\Extension\Core\Type\SubmitType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolver; 

class DesignFormType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $Styles=$options["data"][0]['Style']; 
     $Themes=$options["data"][0]['Themes']; 
     dump($Styles); 
     dump($Themes); 

     /*foreach ($options["data"][0]['Style'] as $style) { 

      $explode = explode('.', $style); 
      $Styles[$explode[0]] = $style; 
     } 

     foreach ($options["data"][0]['Themes'] as $theme) { 

      $explode = explode('.', $theme); 
      $Themes[$explode[0]] = $theme; 
     }*/ 


     $builder 
      ->add(
       "Theme", ChoiceType::class, array("expanded" => true, 
       "multiple" => false, 
       'choices' => $Themes, 
       )) 
      ->add(
       "Style", ChoiceType::class, array("expanded" => true, 
       "multiple" => false, 
       'choices' => $Styles, 
       'choice_value' => function ($value, $key){ 
        return $value; 
       }, 
       )) 
      ->add('save', SubmitType::class, array(
       'attr' => array('class' => 'save'))); 

    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 


    } 

    public function getName() 
    { 
     return 'app_bundle_design_form_type'; 
    } 
} 
+0

我可能已經跳到了結論與我的答案:)你能告訴我們一個'$樣式'和'$主題'的例子嗎? –

+1

希望圖片足夠:) – Kira

回答

1

你可以試試這個:

$Themes[]=$options["data"][0]['Themes']; 
$Styles[]=$options["data"][0]['Style']; 

$builder 
    ->add("Theme",ChoiceType::class,array(
     "expanded"=>true, 
     "multiple"=>false, 
     'choices'=>$Themes, 
     'choice_value' => function ($value, $key){ 
      return $value; 
     }, 
    )) 
    ->add("Style",ChoiceType::class,array(
     "expanded"=>true, 
     "multiple"=>false, 
     'choices'=>$Styles, 
     'choice_value' => function ($value, $key){ 
      return $value; 
     }, 
    )) 
    ->add('save',SubmitType::class,array(
     'attr' => array('class' => 'save') 
    )); 

我認爲它應該有效,但我不確定。這是一個簡單的修復。 choice_value可以調用。

+0

感謝您的幫助, m出現此錯誤:警告:AppBundle \ Form \ DesignFormType :: AppBundle \ Form \ {closure}() – Kira

+0

缺少參數2請顯示您的'DesignFormType'的完整代碼。 –

+0

Full Code is added – Kira

1

這是最可能是由於Form重構(refactorization)在v2.7 happended。

簡短的回答:請務必按以下格式提供您選擇:

[ 
    'text_to_show0' => 'value0', 
    'text_to_show1' => 'value1' 
    .... 
    'text_to_showN' => 'valueN' 
] 

array_flip()可能對你有用這裏。

龍(ER)答案:你可以閱讀所有關於表單組件here

希望這有助於重構(refactorization)...

+0

好吧非常感謝你,我希望這是一個更快的解決方案:),也許陣列翻轉可以工作,但然後我必須翻轉兩次... – Kira