listener - How to add a calculated field in the FormEvents::PRE_SUBMIT Event -
in listener need access entity when in formevents::pre_submit event. in post_set_data no problem, use $event->getdata();.
so event listening post_set_data fine code:
public function postsetdata(formevent $event) { $form = $event->getform(); $day = $event->getdata(); $total = $this->daymanager->calculatetotal($day); // passing day object, yay! $form->get('total')->setdata($total); } however in method pre_submit event.
i need function because on submitting, total not calculated newly submitted data.
public function presubmit(formevent $event) { $form = $event->getform(); // $day = $event->getdata(); // raw array because $event->getdata(); holds old not updated day object $day = $form->getdata(); // ough! had create , call seperate function on daymanager handles raw event array $total = $this->daymanager->calculatetotalfromarray($event->getdata()); // modify event data $data = $event->getdata(); // ough(2)! have nasty things match raw event array notation $totalarray = array( 'hour' => $total->format('g') . "", 'minute' => intval($total->format('i')) . "" ); $data['total'] = $totalarray; $event->setdata($data); } as can see, works. such hackish way, not believe pro's way. 2 things go wrong here:
- cannot work entity
dayobject inpresubmitfunction - have create
calculatetotalfromarrayfunction indaymanager - ugly code match raw event array in
presubmitfunction
so main question: how updated day object form in pre_submit form event.
use submit instead of pre_submit
don't worry, form not yet submitted, submit executed right before form::submit
why having problem?
all data in pre_submit has not been normalized usual object...
if you'd learn more whole thing, please head to: http://symfony.com/doc/current/components/form/form_events.html
Comments
Post a Comment