显示值而不是键

问题描述:

我创建了一个多选表单,但不是键值,而是显示了键,在后端一切正常,可以显示值而不是键,这是表单代码:显示值而不是键

$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

你可以试试这个:

$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

这是最可能是由于Form重构(refactorization)在v2.7 happended。

简短的回答:请务必按以下格式提供您选择:

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

array_flip()可能对你有用这里。

龙(ER)答案:你可以阅读所有关于表单组件here

希望这有助于重构(refactorization)...

+0

好吧非常感谢你,我希望这是一个更快的解决方案:),也许阵列翻转可以工作,但然后我必须翻转两次... – Kira