0

I'm trying to fill my ChoiceType with an array, but it looks like it's filled with the IDs and not with the values. The form is correctly displayed, but the choices are '0', '1'... instead of the names in the array.

This is my controller:

$categories = $this->getDoctrine()->getRepository('myBundle:Category')->findAll();

    $techChoices = array();
    $i = 0;
    foreach($categories as $t) {
        $techChoices[$i] = $t->getName();
        $i = $i + 1;
    }

    $formOptions = array('categories' => $techChoices);


    $document = new Document($categories);
    $form = $this->createForm(DocumentType::class, $document, $formOptions);

And this is my buildForm:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('file', FileType::class)
        ->add('file_name', TextType::class)
        ->add('file_description', TextType::class)
        ->add('file_group', ChoiceType::class, array(
            'choices'  =>  $options['categories'],
        ));
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'categories' => array(),
    ));
}

4 Answers 4

1

Depending on your Symfony's version (since 2.8), you are building the choice array the wrong way.

From 3.3 documentation :

... where the array key is the item's label and the array value is the item's value.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm doing like the 'choices' example, I'm just trying to pass an existing array instead of declaring a new one.
0

If you want to use an array directly in form choice type then see https://symfony.com/doc/current/reference/forms/types/choice.html#example-usage, or if you want to use data from a table(entity) then see https://symfony.com/doc/current/reference/forms/types/entity.html#basic-usage

Answer to your question is the array format should be like

['data_to_be_seen1' => value1(id), 'data_to_be_seen2' => value2(id),...]

(see the first link),

all the best

1 Comment

Thanks a lot, the second link is exactly what I needed. It works now
0

The correct way to show the categories in your case is using EntityType, this will free up your code mess. You don't have to get/pass categories to form anymore.

public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
                ->add('file', FileType::class)
                ->add('file_name', TextType::class)
                ->add('file_description', TextType::class)
                ->add('file_group', \Symfony\Bridge\Doctrine\Form\Type\EntityType::class, array(
                    // query choices from this entity
                    'class' => 'AppBundle:Category',
                    'choice_label' => 'name', 
                ))

        ;
    }

Comments

-1

you can directly do this :

$builder
    ->add('file', FileType::class)
    ->add('file_name', TextType::class)
    ->add('file_description', TextType::class)
    ->add('file_group', ChoiceType::class, array(
        'choices'  =>  'here you pass your categories entities directly',
        'choice_label' => 'name',
    ));

like that, it will do the mapping alone

3 Comments

What do you mean by "here you pass your categories entities directly"? I tried with the array
why do you recreate an array ? just pass the findAll result
I did it because I tried to follow the answer to someone who had the same problem, but it's the same when I'm passing the findAll result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.