2

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?

2 Answers 2

12

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) }}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! And how do I use this in the view?
Thanks! Is it also possible to add the id of the region?
by default the primary key will be used, so your regionId will become the id property in the select box
0

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

Comments

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.