我創建了一個多選表單,但不是鍵值,而是顯示了鍵,在後端一切正常,可以顯示值而不是鍵,這是表單代碼:顯示值而不是鍵
$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')
));
在小枝我只是使用開始和結束小枝命令來啓動窗體。 非常感謝。 從風格和主題轉儲
我修復了一個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';
}
}
我可能已經跳到了結論與我的答案:)你能告訴我們一個'$樣式'和'$主題'的例子嗎? –
希望圖片足夠:) – Kira