0

I have a problem trying to select the default value in a ComboBox (html select tag) usign Symfony2 FormBuilder. Here is my code:

MyController.php I send to the Form th default province to be selected

$n = new Foo();

$em = $this->getDoctrine()->getEntityManager();
$province = $em->getRepository('MyEntityBundle:SYS_TProvince')->find('ES-M');

$form = $this->createForm(new NewsletterType($province), $n);

$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
    $form->bindRequest($request);

    if ($form->isValid()) {
        // some action
    }
}

NewsletterType.php I use the default province in the province field

class NewsletterType extends AbstractType
{
    private $province;

    function __construct($province)
    {
        $this->province = $province;
    }

    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('idnewsletter', 'hidden');
        $builder->add('email', 'email');
        $builder->add('type', 'entity', 
            array('label' => 'type',
                'class' => 'MeediamSplashBundle:USR_TType',
                'property' => 'description',
                'preferred_choices' => array(3,5,7)
            ));
        $builder->add('province', 'entity', 
            array('label' => 'province',
                'class' => 'MeediamSplashBundle:SYS_TProvince',
                'property' => 'name',
                'data' => $this->province
            ));
        $builder->add('postalcode');
        $builder->add('status', 'hidden');
        $builder->add('created', 'hidden');
    }

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

SYS_TProvince.php The entity

<?php

namespace SciOf\Meediam\SplashBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="SYS_TProvince")
 */
class SYS_TProvince
{
    /**
     * @ORM\Id
     * @ORM\Column(type="string", length=5, nullable=false)
     */
    protected $idprovince;

    /**
     * @ORM\Column(type="string", length=3, nullable=false)
     * @Assert\NotBlank()
     */
    protected $idcountry;

    /**
     * @ORM\Column(type="string", length=60, nullable=false)
     * @Assert\NotBlank()
     */
    protected $name;

    public function getIdprovince()             { return $this->idprovince; }
    public function getIdcountry()              { return $this->idcountry; }
    public function getName()                   { return $this->name; }
    public function setIdprovince($idprovince)  { $this->idprovince = $idprovince; }
    public function setIdcountry($idcountry)    { $this->idcountry = $idcountry; }
    public function setName($name)              { $this->name = $name; }

    public function __toString()                { return $this->idprovince; }

}

Apparently every thing is Ok, but it does not work. If I use "preferred_choices", it works, but I can not select a default value via "data".

The object is in the class well, if I use ->getIdProvice() I get the PK of the object, and an error because is a string.

I read some info, but I do not know how to do:

How to set default value for form field in Symfony2? http://symfony.com/doc/current/reference/forms/types/field.html

Does someone see any error?

Community
  • 1
  • 1
unairoldan
  • 2,775
  • 4
  • 28
  • 49

1 Answers1

2

if you want a default value you need to set your default value in your entity before creating the form.

Like $yourEntity->setProvince('my default value');

But in your case i'm not sure about the setter, can you add your entities please?

Snroki
  • 2,424
  • 1
  • 20
  • 28
  • I have posted the entity. Where i have to add this code? Controller? FormBuilder? – unairoldan Apr 20 '12 at 14:12
  • oups sorry to be late, you've to add this before your `$form = $this->createForm(new NewsletterType($province), $n);` in your case $entity should be $n and you just have to set your province (or multiples if you have a relation between the 2 entities), hope i'm clear. – Snroki Apr 23 '12 at 13:51
  • It works. Thanks....but, out of curiosity, and if I'm using a Form without Entity class?? – unairoldan May 19 '12 at 18:33
  • good question. I don't know, never try it but i think even without an entity you have to create a fake object to create your form. So maybe you can do the same as with an entity... – Snroki May 21 '12 at 08:22
  • I have did exactly this, create a fake "Form Entity" to use in with the FormBuilder. – unairoldan May 21 '12 at 15:54