Kira
Kira

Reputation: 47

Display value instead of key

I created a multiple choice form, but instead of the values, the keys are shown, in the backend everything works fine,is it possible to display the values instead of the key, this is the form code:

$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')
    ));

In Twig I'm just using the start and end twig command to start the form. Thank you very much. Dump from Style and Themes Dump from Style and Themes

I fixed it with an foreach loop, but i think its not the best solution?

Edit: Full Code

    <?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';
    }
}

Upvotes: 0

Views: 1332

Answers (2)

Alvin Bunk
Alvin Bunk

Reputation: 7764

Can you try this:

$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')
    ));

I think it should work, but I'm not certain. It's an easy fix. The choice_value is callable.

Upvotes: 1

Jovan Perovic
Jovan Perovic

Reputation: 20191

That is most probably due to Form refactorization that happended in v2.7.

Short answer: Make sure you supply your choices in the following format:

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

array_flip() might be useful to you here.

Long(er) answer: You can read all about refactorization in form component here

Hope this help...

Upvotes: 1

Related Questions