forms - Maintain posted order in symfony2 choice input field (with choice list) -
i'm using symfony2 framework in project , use form component create forms. i'm using choice input field type enable users multi select options , i'm using plugin enable users order these options. unfortunately order of these options isn't maintained when posting form controller. request has correct order form component uses order of choices
option.
how can maintain posted order using form component , choice input field type?
for record, did search on google, stackoverflow , @ github , found issue keeping order of preferred_choices
(https://github.com/symfony/symfony/issues/5136). issue speak sort
option can't find option in symfony2 documentation.
i tried solve same problem : needed select several organizations , sort them in list.
and after $form->getdata()
order request changed.
i made form event handlers , found data have right order on formevents::pre_submit
event , saved in $this->presubmitdata
.
after that, on formevents::submit
event overwrite data wrong order (in real, depends on order choices option) $this->presubmitdata
. (you can remove array_merge method)
class priorityorganizationsettingstype extends abstracttype { private $presubmitdata; /** * @param formbuilderinterface $builder * @param array $options * @throws \exception */ public function buildform(formbuilderinterface $builder, array $options) $builder ->add('organizations', 'choice', array( 'multiple' => 'true', 'required' => false, 'choices' => $this->getpriorityoperatorchoices(), 'attr' => [ 'class' => 'multiselect-sortable', 'style' => 'height: 350px; width:100%;' ] )) ; $builder->addeventlistener(formevents::submit, array($this, 'submitevent')); $builder->addeventlistener(formevents::pre_submit, array($this, 'presubmitevent')); } public function presubmitevent(formevent $event) { $this->presubmitdata = $event->getdata(); } public function submitevent(formevent $event) { $event->setdata(array_merge( $event->getdata(), $this->presubmitdata )); } }
Comments
Post a Comment