I use form classes to create various forms in my project.
In the Entity file for the buildForm function, there is a secondary parameter "array $ options" (this is shown in the official Symfony 2 documentation, but never mentioned!)
I fed the array to the createForm function in my controller, but now I'm completely fixated on how to get the stored values?
$form = $this->createForm(new ProductType(array(), array('id' => '2')), $product);
The only thing I found about getting parameters was using the getOption () function, but this does not exist in Symfony 2 (the message I found was from 2010).
I tried using:
$id = $options['id'];
But when I try to use $ id anywhere, I get an error:
Note: Undefined index: id
What gives?
How to get my values from $ options array? Do I really install them correctly?
EDIT - Additional Code:
Form class
<?php namespace DEMO\DemoBundle\Form\Product; use Doctrine\ORM\EntityRepository; 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('slug') ->add('reference') ->add('description') ->add('active_from') ->add('active_till') ->add('is_active') ->add('category', 'entity', array( 'class' => 'DEMO\DemoBundle\Entity\Product\ProductCategory', 'query_builder' => function(EntityRepository $er) { return $er->createQueryBuilder('u') ->where('u.section = :id') ->setParameter('id', ***ID VARIABLE NEEDS TO GO HERE***) ->orderBy('u.root', 'ASC') ->addOrderBy('u.lft', 'ASC'); }, 'empty_value' => 'Choose an option', 'property' => 'indentedName', )); } public function getDefaultOptions() { return array( 'data_class' => 'DEMO\DemoBundle\Entity\Product\Product' ); } public function getName() { return 'demo_demobundle_product_type'; } }
source share