Skip to content

Commit

Permalink
Fixed submit buttons using FormEvent
Browse files Browse the repository at this point in the history
Fixed a bug where form submit buttons were added at the beginning of the form
  • Loading branch information
Jérémy James authored and Tom32i committed Jan 19, 2015
1 parent 3e5be57 commit 05253b7
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions Form/Extension/FormButtonTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

namespace Elao\Bundle\FormBundle\Form\Extension;

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
Expand Down Expand Up @@ -41,13 +43,16 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$buttons = self::getButtons();
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) {
$buttons = self::getButtons();
$form = $event->getForm();

foreach ($buttons as $button) {
if (isset($options[$button]) && $options[$button] && !$builder->has($button)) {
$builder->add($button, $button);
foreach ($buttons as $button) {
if (isset($options[$button]) && $options[$button] && !$form->has($button)) {
$form->add($button, $button);
}
}
}
});
}

/**
Expand Down

0 comments on commit 05253b7

Please sign in to comment.