nielsv
nielsv

Reputation: 6800

Fill select list in form with values from database + Symfony 2 & Doctrine

I want to create a form with to begin only a select list wtih values selected from a database.
This is the entity Region and I would like to fill the dropdown with regions.

<?php

namespace Reuzze\ReuzzeBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Regions
 *
 * @ORM\Table(name="regions")
 * @ORM\Entity
*/
class Regions
{
    /**
     * @var integer
     *
     * @ORM\Column(name="region_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $regionId;

    /**
     * @var string
     *
     * @ORM\Column(name="region_name", type="string", length=45, nullable=false)
     */
     protected $regionName;



    /**
     * Get regionId
     *
     * @return integer 
     */
    public function getRegionId()
    {
        return $this->regionId;
    }

    /**
     * Set regionName
     *
     * @param string $regionName
     * @return Regions
     */
    public function setRegionName($regionName)
    {
        $this->regionName = $regionName;

        return $this;
    }

    /**
     * Get regionName
     *
     * @return string 
     */
    public function getRegionName()
    {
        return $this->regionName;
    }
}

This is my form:

class RegionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('regionName'   , 'text', array(
            'label' => 'Region Name',
            'attr' => array('placeholder' => 'Region Name')
        ));
    }

    public function getName()
    {
        return 'region';
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Reuzze\ReuzzeBundle\Entity\Regions',
        ));
    }

}

But now I would like to show the regions in a select list instead of giving in the name in a textbox. Does anybody know how I can do this in my form? And how I show it in my view?

Upvotes: 2

Views: 17173

Answers (2)

t j
t j

Reputation: 7294

Updated answer for Symfony 3+:

You now need to use the EntityType class and the choice_label option.

See docs here: http://symfony.com/doc/current/reference/forms/types/entity.html

use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Reuzze\ReuzzeBundle\Entity\Regions;

class RegionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(
            'regionName',
            EntityType::class,
            [
                'class' => Regions::class,
                'choice_label' => 'regionName',
                'expanded' => false,
                'multiple' => false
            ]
        );

        $builder->setMethod('POST');

        return $builder;
    }
}

Upvotes: 0

Sehael
Sehael

Reputation: 3736

This is clearly documented in the Symfony docs - Choice Field

$builder->add('regions', 'entity', array(
    'class' => 'ReuzzeReuzzeBundle:Regions',
    'property' => 'regionName',
    'expanded' => false,
    'multiple' => false
));

Something like this should get you going.

NOTE This code would be placed within a form that will have the select box. You probably don't want to render only a select box with nothing else.

UPDATE

The docs also show you how to render a form in a template.

In your controller:

$region = new Region();
$form = $this->createForm(new RegionType(), $region ); //or create a different form

// ....

return $this->render('ReuzzeReuzzeBundle:Default:index.html.twig', array(
        'form' => $form->createView(),
));

And in a twig template index.html.twig:

{{ form_start(form) }}
    {{ form_errors(form) }}

    {{ form_row(form.regions) }
{{ form_end(form) }}

Upvotes: 12

Related Questions