Aurel
Aurel

Reputation: 796

Symfony2 : displaying form entity field type as radio buttons

I am trying to display all categories (from my table Category) with radio buttons. There are 3 categories but only one radio button is displayed while the Doctrine query is OK.

My FormType code :

public function buildForm(FormBuilder $builder, array $options)
{
    $builder->add('categories', 'entity', array(
        'class' => 'MyBundle:Category',
        'expanded' => true,
        'property' => 'title',
        'property_path' => false,
    ));
}

My Twig code :

{{ form_widget(form) }}

The result should return 3 radio buttons, but I see only one radio button (the first in my table).

The Doctrine query displayed in the Symfony profiler (debug bar) is executed perfectly and returns 3 rows.

Maybe the problem comes from the "property_path" that I had to add, otherwise I had an exception :

Neither property "categories" nor method "getCategories()" nor method "isCategories()" exists in class ...

Any help please ?

Many thanks :-)

Aurel

EDIT :

Here is my dev.log corresponding to this request, when removing the "property_path" :

[2012-04-26 07:27:01] doctrine.DEBUG: SELECT t0.id AS id1, t0.last_update AS last_update2, t0.title AS title4 FROM category t0 ([]) [] []
[2012-04-26 07:27:01] event.DEBUG: Notified event "kernel.exception" to listener "Symfony\Component\Security\Http\Firewall\ExceptionListener::onKernelException". [] []
[2012-04-26 07:27:01] event.DEBUG: Notified event "kernel.exception" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelException". [] []
[2012-04-26 07:27:01] event.DEBUG: Notified event "kernel.exception" to listener "Symfony\Component\HttpKernel\EventListener\ExceptionListener::onKernelException". [] []
[2012-04-26 07:27:01] request.CRITICAL: Symfony\Component\Form\Exception\InvalidPropertyException: Neither property "categories" nor method "getCategories()" nor method "isCategories()" exists in class "Acme\MyBundle\Entity\Category" (uncaught exception) at /www/vendor/symfony/src/Symfony/Component/Form/Util/PropertyPath.php line 316 [] []

I really don't understand why it refused to display each row of my Category table...

Upvotes: 3

Views: 3088

Answers (1)

Aurel
Aurel

Reputation: 796

The problem was that my Entity was not properly set. The $id field was a boolean instead of being an integer.

Upvotes: 1

Related Questions