Skip to content

Instantly share code, notes, and snippets.

@andreia
Forked from onema/gist:6060077
Created August 27, 2014 02:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreia/e11f997cf90195a5cb29 to your computer and use it in GitHub Desktop.
Save andreia/e11f997cf90195a5cb29 to your computer and use it in GitHub Desktop.
<?php
namespace Acme\DemoBundle\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
/**
* Changes Form->submit() behavior so that it treats not set values as if they
* were sent unchanged.
*
* Use when you don't want fields to be set to NULL when they are not displayed
* on the page (or to implement PUT/PATCH requests).
* @link https://gist.github.com/makasim/3720535 for more information
*/
class PatchSubscriber implements EventSubscriberInterface
{
public function onPreSubmit(FormEvent $event)
{
$form = $event->getForm();
$clientData = $event->getData();
$clientData = array_replace($this->prepareData($form), $clientData ?: array());
$event->setData($clientData);
}
/**
* Returns the form's data like $form->submit() expects it
*/
protected function prepareData($form)
{
if ($form->count()) {
$data = array();
foreach ($form->all() as $name => $child) {
$data[$name] = $this->prepareData($child);
}
return $data;
} else {
return $form->getViewData();
}
}
static public function getSubscribedEvents()
{
return array(
FormEvents::PRE_SUBMIT => 'onPreSubmit',
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment