symfony2 - Passing options array to EventListerner from a FormType -



symfony2 - Passing options array to EventListerner from a FormType -

i have formtype reused editing & creating records. form has 1 entity field renders select populated depending record id, need skip field when creating new record. read http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html , after changing roots, seems fine until i'm trying edit existing record when stuck error:

contexterrorexception: notice: undefined variable: options in /users/a77/documents/dev/uvox com/src/acme/demobundle/eventlistener/venuefieldsubscriber.php line 32

mi venuesformtype :

namespace acme\demobundle\form; utilize symfony\component\form\abstracttype; utilize symfony\component\form\formbuilderinterface; utilize symfony\component\optionsresolver\optionsresolverinterface; utilize acme\demobundle\eventlistener\venuefieldsubscriber; class venuestype extends abstracttype { public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('name', 'text') ->add('password', 'text') ->add('save', 'submit', array('label' => 'save', 'attr' => array('data-loading-text' => 'loading', 'class' => "btn btn-primary"))) ->addeventsubscriber(new venuefieldsubscriber()); } /** * @param optionsresolverinterface $resolver */ public function setdefaultoptions(optionsresolverinterface $resolver) { $resolver->setdefaults(array( 'data_class' => 'acme\demobundle\entity\venues' )); } /** * @return string */ public function getname() { homecoming 'acme_demobundle_venues'; } }

and venuefieldsubscriber :

namespace acme\demobundle\eventlistener; utilize symfony\component\form\formevent; utilize symfony\component\form\formevents; utilize symfony\component\eventdispatcher\eventsubscriberinterface; utilize symfony\component\optionsresolver\optionsresolverinterface; utilize doctrine\orm\entityrepository; class venuefieldsubscriber implements eventsubscriberinterface { public static function getsubscribedevents() { // tells dispatcher want hear on form.pre_set_data // event , presetdata method should called. homecoming array(formevents::pre_set_data => 'presetdata'); } public function presetdata(formevent $event) { $product = $event->getdata(); $form = $event->getform(); if (!$product || null === $product->getid()) { // no action new record } else { $form->add('user', 'entity', array( 'class' => 'acmedemobundle:users', 'property' => 'username', 'query_builder' => function(entityrepository $er) utilize ($options) { homecoming $er->createquerybuilder('u') ->where('u.venue=?1') ->andwhere('u.usertype >1') ->orderby('u.username', 'asc') ->setparameter(1, $options['attr']['venueid']); } )); } }

any ideas, i'm missing ? $options['attr']['venueid'] should give me id of record i'm editing... ;)

i assume after

$product = $event->getdata();

$product object of acme\demobundle\entity\venues class.

then instead of

->setparameter(1, $options['attr']['venueid'])

try

->setparameter(1, $product->getid())

symfony2

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -