1

Hi I am so new in symfony2. And I'm having a hard time figuring out what to do with my select box. I need to use an array of "positions" from my database and use it as choices for my select box. Since this project is nearly due, I'd really appreciate any help from you guys.

Atm I have the following codes for my form setup:

        $role = $query1->getResult(Query::HYDRATE_ARRAY);

        $roles = array();
        for($i = 0; $i <count($roles); $i++){
            $roles[] = $role[$i]['disrole'];
        }

        $form = $this->createFormBuilder($position)
            ->add('position', 'choice', array('choices'  => $roles))
            ->add('save', 'submit', array('label' => 'Search'))->setMethod('POST')
            ->getForm();

And here's how I use it in my twig template:

<div class="panel-body">
   {{ form(form) }}
</div>

I simply output my form like that cause I'm not very familiar with splicing up the form parts. I'd really really appreciate your answers guys! Thank you in advance!

2
  • What's your Symfony version? There are some big changes in the latest 2.7 release regarding the choices handling. Commented Jun 29, 2015 at 7:08
  • Hi my version is 2.7 but I fivmgured something out but it seems to be inefficient. The voted answer below looks good and I'll try it. Thanks! Commented Jun 29, 2015 at 15:21

2 Answers 2

2

Instead of a choice field you could use an entity field, which is a choice field designed to load its options from Doctrine :

$form->add(
    'position',
    'entity',
    [
        'class' => 'YourBundle:Role',
        'query_builder' => function (RoleRepository $repository) {
            $queryBuilder = $repository->createQueryBuilder('role');
            // create your query here, or get it from your repository
            return $queryBuilder;
        },
        'choice_label' => 'disrole'
    ]
);
Sign up to request clarification or add additional context in comments.

1 Comment

Hi! Thank youuu soo much this worked for me! Savior! And thank you for teaching me how to get use of symfony's features my temporary solution for this one was a non-entity form submitted and validated using globals. Thanks again! I wish I could upvote your anwer but my reputation isnt enough.
0

To use choices in a form I'd use the following possibility:

/**
 * @param FormBuilderInterface $builder
 * @param array                $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('stuff', 'choice', ['choices' => $this->getChoices()]);
}


private function getChoices()
{
    $contents = $this->someRepoInjectedInForm->findChoices();

    $result = [];
    foreach ($contents as $content) {
        $result[$content->getId()] = $content->getLabel();
    }

    return $result;
}

Your choice label will be the array value, and the value which is send to your backend via form will be the key of the corresponding key-value-pair in your array.

2 Comments

Hi DerStoffel! Thank you for taking the time to help me out. But I think the entity field would be better. But hey I almost resorted to this solution. Anywayy, Thanks buddy!
Well, ofc entity can be used as well, but this is not what you asked. You asked for choice as array!! Therefore you got an answer for that. Pls ask more precicely in the future.

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.