Skip to content

Instantly share code, notes, and snippets.

@jmikola
Created December 12, 2011 22:18
Show Gist options
  • Save jmikola/1469399 to your computer and use it in GitHub Desktop.
Save jmikola/1469399 to your computer and use it in GitHub Desktop.
Using Symfony2 form events to prevent overwriting fields with empty values
diff --git a/src/Application/UserBundle/Form/UserEditFormType.php b/src/Application/UserBundle/Form/UserEditFormType.php
index ea5bf44..150da51 100644
--- a/src/Application/UserBundle/Form/UserEditFormType.php
+++ b/src/Application/UserBundle/Form/UserEditFormType.php
@@ -3,7 +3,9 @@
namespace Application\UserBundle\Form;
use Symfony\Component\Form\FormBuilder;
+use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\AbstractType;
+use Symfony\Component\Form\Event\FilterDataEvent;
use Exercise\CommonBundle\Form\MonthType;
use Exercise\CommonBundle\Form\DayType;
use Exercise\CommonBundle\Form\YearType;
@@ -53,7 +55,14 @@ class UserEditFormType extends AbstractType
))
->add('website')
->add('occupation')
- ->add($builder->create('aboutMe', 'textarea')->appendClientTransformer(new ApiTransformer()))
+ ->add($builder
+ ->create('aboutMe', 'textarea')
+ ->addEventListener(FormEvents::BIND_NORM_DATA, function(FilterDataEvent $event) {
+ if (null === $event->getData()) {
+ $event->setData($event->getForm()->getData());
+ }
+ })
+ )
;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment