Skip to content

Instantly share code, notes, and snippets.

@chmielot
Created May 25, 2011 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chmielot/990760 to your computer and use it in GitHub Desktop.
Save chmielot/990760 to your computer and use it in GitHub Desktop.
[Form] Boolean choice field not preselected
<?php
namespace BUM\TestBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use BUM\TestBundle\Form\FooType;
use BUM\TestBundle\Entity\Foo;
/**
* @Route("/formtest")
*/
class DefaultController extends Controller
{
/**
* @Route("/", name="formtest_index")
* @Template()
*/
public function indexAction()
{
$em = $this->get('doctrine')->getEntityManager();
$items = $em->getRepository('BUMTestBundle:Foo')->findAll();
if (!$items) {
throw new NotFoundHttpException('No database entries found! Load fixtures.');
}
$forms = array();
foreach ($items as $item) {
$form = $this->get('form.factory')->create(new FooType(), $item);
$forms[] = $form->createView();
}
/*
if ($this->get('request')->getMethod() == 'POST') {
$form->bindRequest($this->get('request'));
if ($form->isValid()) {
$em->flush();
return $this->redirect($this->generateUrl('formtest_index'));
}
}*/
return array(
'forms' => $forms,
'foo' => $items,
);
}
}
<?php
namespace BUM\TestBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\FixtureInterface;
use BUM\TestBundle\Entity\Foo;
class LoadFooData implements FixtureInterface
{
public function load($manager)
{
$foo = new Foo();
$foo->setName('name1 - inactive');
$foo->setActive(0);
$manager->persist($foo);
$foo2 = new Foo();
$foo2->setName('name2 - active');
$foo2->setActive(1);
$manager->persist($foo2);
$manager->flush();
}
}
<?php
namespace BUM\TestBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="test")
*/
class Foo
{
/**
* @var integer
*
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(type="string", length="50")
*/
protected $name;
/**
* @var boolean
*
* @ORM\Column(type="boolean", nullable=false)
*/
protected $active;
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function setActive($active)
{
$this->active = $active;
}
public function getActive()
{
return $this->active;
}
}
<?php
namespace BUM\TestBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class FooType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('name')
->add('active', 'choice', array(
'label' => 'Active',
'required' => false,
'expanded' => true,
'multiple' => false,
'choices' => array( 0 => 'no', 1 => 'yes')))
;
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'BUM\TestBundle\Entity\Foo',
);
}
}
test_bundle:
resource: "@BUMTestBundle/Controller/DefaultController.php"
type: annotation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment