Using Symfony's Form Event to remove some of the data sent to a form -
i have checkboxes , want find out items checked checkboxes , send them database. made this:
class notneededfieldssubscriber implements eventsubscriberinterface { public static function getsubscribedevents() { return array(formevents::pre_bind => 'prebinddata'); } public function prebinddata(formevent $event) { $data = $event->getdata(); $count = count($data['items']); ($i=0; $i < $count; $i++){ if (!array_key_exists('enabled', $data['items'][$i])){ unset($data['items'][$i]); } } $event->setdata($data); } }
when test see $event->getdata has want.
in controller:
$form = $this->formfactory->create(new itemtype(), $item); if ($request->ismethod('post')) { $form->bind($request); if ($form->isvalid()) { $this->em->persist($item); $this->em->flush(); } } return $this->redirect($this->router->generate('home'));
the problem still checked , unchecked items in database :(
any ideas why , how fix this? in advance! :)
as checkbox in fact collection, maybe should refer part of documentation : http://symfony.com/doc/current/cookbook/form/form_collections.html#allowing-tags-to-be-removed
specially "doctrine: ensuring database persistence" part. have manually remove enities still stored in database not in form anymore
// filter $originaltags contain tags no longer present foreach ($task->gettags() $tag) { foreach ($originaltags $key => $todel) { if ($todel->getid() === $tag->getid()) { unset($originaltags[$key]); } } } // remove relationship between tag , task foreach ($originaltags $tag) { // remove task tag $tag->gettasks()->removeelement($task); // if manytoone relationship, remove relationship // $tag->settask(null); $em->persist($tag); // if wanted delete tag entirely, can // $em->remove($tag); }
Comments
Post a Comment