Catchable Fatal Error in form event subscriber using Symfony 2.3 -
after updating symfony 2.3 2.1, there strange message when trying create form :
catchable fatal error: argument 1 passed msgr\profilebundle\form\eventlistener\addnamefieldsubscriber::presetdata() must instance of symfony\component\form\event\dataevent, instance of symfony\component\form\formevent given in /xxxxxx/aaas/src/msgr/profilebundle/form/eventlistener/addnamefieldsubscriber.php line 29
these usual suspects :
profiletype.php
<?php namespace aaas\profilebundle\form; use doctrine\orm\entityrepository; use symfony\component\form\formfactoryinterface; use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface; use symfony\component\optionsresolver\optionsresolverinterface; use aaas\profilebundle\form\eventlistener\addnamefieldsubscriber; class profiletype extends abstracttype { public function buildform(formbuilderinterface $builder, array $options) { $subscriber = new addnamefieldsubscriber($builder->getformfactory()); $builder->addeventsubscriber($subscriber); $builder ->add('firstname') ->add('lastname') ->add('city') ->add('country') ->add('mobile') ->add('description') ->add('avatarfile') ->add('website') ->add('facebook') ->add('twitter') ->add('googleplus') ->add('gender', 'choice', array( 'choices' => array('m' => 'man', 'f' => 'female'), 'required' => true, 'empty_value' => 'choose gender', )) ; } //public function getparent() //{ // return 'form'; //} public function setdefaultoptions(optionsresolverinterface $resolver) { $resolver->setdefaults(array( 'data_class' => 'aaas\profilebundle\entity\profile' )); } public function getname() { return 'profile'; } }
addnamefieldsubscriber.php
<?php // src/acme/demobundle/form/eventlistener/addnamefieldsubscriber.php namespace aaas\profilebundle\form\eventlistener; use symfony\component\form\formfactoryinterface; use doctrine\orm\entitymanager; use symfony\component\eventdispatcher\eventsubscriberinterface; use symfony\component\form\formevents; use symfony\component\form\event\dataevent; class addnamefieldsubscriber implements eventsubscriberinterface { private $factory; public function __construct(formfactoryinterface $factory) { $this->factory = $factory; } public static function getsubscribedevents() { // tells dispatcher want listen on form.pre_set_data // event , presetdata method should called. return array(formevents::pre_set_data => 'presetdata'); } public function presetdata(dataevent $event) { //$data = $event->getdata(); $form = $event->getform(); // during form creation setdata() called null argument // formbuilder constructor. we're concerned when // setdata called actual entity object in (whether new, // or fetched doctrine). if statement let's skip right // on null condition. if (null === $data) { return; } // check if product object "new" if (!$data->getid()) { $form->add($this->factory->createnamed('username','text', array( 'auto_initialize' => false ))); $form->add($this->factory->createnamed('password','password')); $form->add($this->factory->createnamed('email','email')); $form->add($this->factory->createnamed('groups', 'entity',null, array( 'multiple' => false, 'class' => 'aaasprofilebundle:group', 'query_builder' => function(entityrepository $er) { return $er->createquerybuilder('g') ->where('g.id != 1') ->andwhere('g.id != 2') ->andwhere('g.id != 3'); } ))); } } }
unfortunately, cannot find root cause of problem !
your class addnamefieldsubscriber
has method public function presetdata(dataevent $event)
should instead typehinting formevent
. form component changed event object dispatched between 2.1 , 2.3:
<?php // src/acme/demobundle/form/eventlistener/addnamefieldsubscriber.php namespace aaas\profilebundle\form\eventlistener; use symfony\component\form\formfactoryinterface; use doctrine\orm\entitymanager; use symfony\component\eventdispatcher\eventsubscriberinterface; use symfony\component\form\formevents; use symfony\component\form\formevent; class addnamefieldsubscriber implements eventsubscriberinterface { private $factory; public function __construct(formfactoryinterface $factory) { $this->factory = $factory; } public static function getsubscribedevents() { // tells dispatcher want listen on form.pre_set_data // event , presetdata method should called. return array(formevents::pre_set_data => 'presetdata'); } public function presetdata(formevent $event) { //$data = $event->getdata(); $form = $event->getform(); // during form creation setdata() called null argument // formbuilder constructor. we're concerned when // setdata called actual entity object in (whether new, // or fetched doctrine). if statement let's skip right // on null condition. if (null === $data) { return; } // check if product object "new" if (!$data->getid()) { $form->add($this->factory->createnamed('username','text')); $form->add($this->factory->createnamed('password','password')); $form->add($this->factory->createnamed('email','email')); $form->add($this->factory->createnamed('groups', 'entity',null, array( 'multiple' => false, 'class' => 'aaasprofilebundle:group', 'query_builder' => function(entityrepository $er) { return $er->createquerybuilder('g') ->where('g.id != 1') ->andwhere('g.id != 2') ->andwhere('g.id != 3'); } ))); } } }
Comments
Post a Comment