Search code examples
formssymfonyentity

symfony form choice with entity get result


I'm trying to get the result of my choice form.

The form is like this :

$users = $em->getRepository('UserBundle:User')->findAll();

    $form = $this->createFormBuilder($users)
    ->add('users', 'entity', array(
        'label' => 'Pick that user',
        'class' => 'UserBundle:User',
        'choice_label' => 'usFirstname'))
        ->add('save', 'submit', array('label' => 'Submit'));

Then I want to get the user picked, I try several things, but nothing worked... It should be something like this :

$user_picked = 
$em->getRepository('UserBundle:User')->
findBy(array('usFirstname' => $form->getForm()->get('users')->getData()));

How should I do to get the user picked after the button 'Submit' is clicked ?


Solution

  • The magic of the form builder does this work for you.
    If you dump the data dump($form->get('users')->getData()) it will be an instance of the user for the choice in the form. No need to to do an extra query.

    Also the choice_label just filters what is in the text of the select option, not the value.

    Example;

    <?php
    namespace AppBundle\Controller;
    
    use AppBundle\Entity\User,
        AppBundle\Form\MyFormType;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route,
        Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter,
        Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
    use Symfony\Component\HttpFoundation\Request,
        Symfony\Bundle\FrameworkBundle\Controller\Controller;
    
    class MyController extends Controller 
    {
        public function addAction(Request $request)
        {
            $form = $this->createForm(new MyFormType(), null, []);
            $form->add('submit', 'submit', [
                    'label' => 'Create',
                    'attr'  => ['class' => 'btn btn-success pull-right']
                ]
            );
            $form->handleRequest($request);
    
            if ($form->isSubmitted() && $form->isValid()) {
                dump($form->get('users')->getData());
                exit(0);
            }
    
            return $this->render('AppBundle:user:form.html.twig', [
                    'form' => $form->createView()
                ]
            );
        }
    }
    
    <?php 
    namespace AppBundle\Form;
    
    use Symfony\Component\Form\AbstractType,
        Symfony\Component\Form\FormBuilderInterface,
        Symfony\Component\OptionsResolver\OptionsResolver,
        Symfony\Component\Validator\Constraints as Assert;
    
        class MyFormType extends AbstractType
        {
    
            /**
             * @param FormBuilderInterface $builder
             * @param array $options
             */
            public function buildForm(FormBuilderInterface $builder, array $options)
            {
                $builder->add('users', 'entity', [
                    'required' => false,
                    'class' => 'AppBundle:User',
                    'choice_label' => 'usFirstname',
                    'expanded' => false,
                    'multiple' => false,
                ]);
            }
    
            /**
             * @param OptionsResolver $resolver
             */
            public function configureOptions(OptionsResolver $resolver)
            {
                $resolver->setDefaults(array(
                    'data_class' => null,
                ));
            }
    
            /**
             * @return string
             */
            public function getName()
            {
                return 'my_form';
            }
    
        }