Home  >  Q&A  >  body text

Symfony2 (2.0.17-DEV) 500 error occurs after creating form type

I made a demo of adding, browsing, updating, and deleting products according to the latest official PDF manual (en Edition). There is a many-to-one category attribute in the product. After previous testing, there is no problem.
src/Acme/StoreBundle/Entity/Product.php

/**
 * @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
 * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
protected $category;

src/Acme/StoreBundle/Entity/Category.php

/**
 * @ORM\OneToMany(targetEntity="Product", mappedBy="category")
*/
protected $products;

Then according to the Form chapter, I transformed the page for adding products into a form to submit to add products:

public function createAction(Request $request)
    {
        $category = $this->getDoctrine()
            ->getRepository('AcmeStoreBundle:Category')
            ->find(1);

        $product = new Product();
        $product->setCategory($category);

        $form = $this->createFormBuilder($product)
            ->add('name')
            ->add('price')
            ->add('description')
            ->getForm();

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

            if($form->isValid())
            {
                $created = true;
                $em = $this->getDoctrine()->getEntityManager();
                $em->persist($product);
                $em->flush();
            }
        }

        return $this->render('AcmeStoreBundle:Default:create.html.twig', array('created' => $created, 'form' => $form->createView(), 'product_id' => $product->getId() ));
    }

At this point, all links can work normally, and the database can also create successful data. Then I saw "Creating Form Classes", so I created a Form class using the doctrine:generate:form that comes with the command line:

php app/console doctrine:generate:form AcmeStoreBundle:Product

src/Acme/StoreBundle/Form/ProductType.php

namespace Acme\StoreBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class ProductType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('name') 
            ->add('price') 
            ->add('description') 
            ->add('category')
        ;
    }

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

Then I modified the controller code according to the manual:

...
use Acme\StoreBundle\Form\ProductType;
...
$form = $this->createForm(new ProductType(), $product);
...

But after clearing the cache, a symfony2 500 error occurred in the access address. The error message in the log is:

[2012-08-26 16:00:27] request.INFO: Matched route "store_create" (parameters: "_controller": "Acme\StoreBundle\Controller\DefaultController::createAction", "_route": "store_create") [] []
[2012-08-26 16:00:27] request.CRITICAL: Symfony\Component\Form\Exception\FormException: Entity "Acme\StoreBundle\Entity\Category" passed to the choice field must have a "__toString()" method defined (or you can also override the "property" option). (uncaught exception) at /work/projects/admin/php/vendor/symfony/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php line 188 [] []

After debugging the echo output content, it is located at the modified sentence "$form = $this->createForm(new ProductType(), $product);"

黄舟黄舟2685 days ago400

reply all(1)I'll reply

  • 迷茫

    迷茫2017-05-16 16:48:23

    The meaning of this error is that when Symfony generates the drop-down menu (<select>), it does not know what should be used as the text content of the option (<option>). The solution has been provided in the error message. Suppose you want to The text displayed is categoryName:

    (1) Add a "__toString()" method to your Entity class;

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

    (2) Specify a field in the Entity as the option text:

    $builder
        ->add('name') 
        ->add('price') 
        ->add('description') 
        ->add('category', 'entity', array(
            // ...
            'property' => 'categoryName'
            // ...
        ))
    ;

    reply
    0
  • Cancelreply