Skip to content

Commit

Permalink
[Form] Merged various form events and added class FormEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
webmozart committed May 25, 2012
1 parent bec8015 commit 33fecca
Show file tree
Hide file tree
Showing 32 changed files with 334 additions and 251 deletions.
29 changes: 29 additions & 0 deletions UPGRADE-2.1.md
Expand Up @@ -671,6 +671,35 @@
$builder->addViewTransformer(new MyTransformer());
```

* The following events were deprecated and have a new equivalent:

* `FormEvents::SET_DATA`: `FormEvents::PRE_SET_DATA`
* `FormEvents::BIND_CLIENT_DATA`: `FormEvents::PRE_BIND`
* `FormEvents::BIND_NORM_DATA`: `FormEvents::BIND`

The deprecated events will be removed in Symfony 2.3.

Furthermore, the event classes `DataEvent` and `FilterDataEvent` were
deprecated and replaced by the generic `FormEvent`. You are advised to
code your listeners against the new event now. The deprecated events will
be removed in Symfony 2.3.

Before:

```
$builder->addListener(FormEvents::BIND_CLIENT_DATA, function (FilterDataEvent $event) {
// ...
});
```

After:

```
$builder->addListener(FormEvents::PRE_BIND, function (FormEvent $event) {
// ...
});
```

### Validator

* The methods `setMessage()`, `getMessageTemplate()` and
Expand Down
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Bridge\Doctrine\Form\EventListener;

use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Event\FilterDataEvent;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
Expand All @@ -30,10 +30,10 @@ static public function getSubscribedEvents()
{
// Higher priority than core MergeCollectionListener so that this one
// is called before
return array(FormEvents::BIND_NORM_DATA => array('onBindNormData', 10));
return array(FormEvents::BIND => array('onBind', 10));
}

public function onBindNormData(FilterDataEvent $event)
public function onBind(FormEvent $event)
{
$collection = $event->getForm()->getData();
$data = $event->getData();
Expand Down
Expand Up @@ -15,7 +15,7 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Event\FilterDataEvent;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
Expand Down Expand Up @@ -57,7 +57,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
* request; however, we can match the expected behavior by checking the
* session for an authentication error and last username.
*/
$builder->addEventListener(FormEvents::SET_DATA, function (FilterDataEvent $event) use ($request) {
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($request) {
if ($request->attributes->has(SecurityContextInterface::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(SecurityContextInterface::AUTHENTICATION_ERROR);
} else {
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Expand Up @@ -124,3 +124,10 @@ CHANGELOG
* `finishView`
in FormTypeInterface and FormTypeExtensionInterface
* [BC BREAK] no options are passed to `getParent` of FormTypeInterface anymore
* deprecated DataEvent and FilterDataEvent in favor of the new FormEvent which is
now passed to all events thrown by the component
* FormEvents::BIND now replaces FormEvents::BIND_NORM_DATA
* FormEvents::PRE_SET_DATA now replaces FormEvents::SET_DATA
* FormEvents::PRE_BIND now replaces FormEvents::BIND_CLIENT_DATA
* deprecated FormEvents::SET_DATA, FormEvents::BIND_CLIENT_DATA and
FormEvents::BIND_NORM_DATA
16 changes: 16 additions & 0 deletions src/Symfony/Component/Form/Event/DataEvent.php
Expand Up @@ -14,6 +14,12 @@
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Form\FormInterface;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Code against
* {@link \Symfony\Component\Form\FormEvent} instead.
*/
class DataEvent extends Event
{
private $form;
Expand Down Expand Up @@ -50,4 +56,14 @@ public function getData()
{
return $this->data;
}

/**
* Allows updating with some filtered data
*
* @param mixed $data
*/
public function setData($data)
{
$this->data = $data;
}
}
15 changes: 6 additions & 9 deletions src/Symfony/Component/Form/Event/FilterDataEvent.php
Expand Up @@ -11,15 +11,12 @@

namespace Symfony\Component\Form\Event;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Code against
* {@link \Symfony\Component\Form\FormEvent} instead.
*/
class FilterDataEvent extends DataEvent
{
/**
* Allows updating with some filtered data
*
* @param mixed $data
*/
public function setData($data)
{
$this->data = $data;
}
}
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\Form\Extension\Core\EventListener;

use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Event\FilterDataEvent;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface;

Expand All @@ -36,7 +36,7 @@ public function __construct(ChoiceListInterface $choiceList)
$this->choiceList = $choiceList;
}

public function onBindClientData(FilterDataEvent $event)
public function preBind(FormEvent $event)
{
$values = (array) $event->getData();
$indices = $this->choiceList->getIndicesForValues($values);
Expand All @@ -46,6 +46,6 @@ public function onBindClientData(FilterDataEvent $event)

static public function getSubscribedEvents()
{
return array(FormEvents::BIND_CLIENT_DATA => 'onBindClientData');
return array(FormEvents::PRE_BIND => 'preBind');
}
}
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\Form\Extension\Core\EventListener;

use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Event\FilterDataEvent;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface;

Expand All @@ -36,7 +36,7 @@ public function __construct(ChoiceListInterface $choiceList)
$this->choiceList = $choiceList;
}

public function onBindClientData(FilterDataEvent $event)
public function preBind(FormEvent $event)
{
$value = $event->getData();
$index = current($this->choiceList->getIndicesForValues(array($value)));
Expand All @@ -46,6 +46,6 @@ public function onBindClientData(FilterDataEvent $event)

static public function getSubscribedEvents()
{
return array(FormEvents::BIND_CLIENT_DATA => 'onBindClientData');
return array(FormEvents::PRE_BIND => 'preBind');
}
}
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\Form\Extension\Core\EventListener;

use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Event\FilterDataEvent;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
Expand All @@ -29,7 +29,7 @@ public function __construct($defaultProtocol = 'http')
$this->defaultProtocol = $defaultProtocol;
}

public function onBindNormData(FilterDataEvent $event)
public function onBind(FormEvent $event)
{
$data = $event->getData();

Expand All @@ -40,6 +40,6 @@ public function onBindNormData(FilterDataEvent $event)

static public function getSubscribedEvents()
{
return array(FormEvents::BIND_NORM_DATA => 'onBindNormData');
return array(FormEvents::BIND => 'onBind');
}
}
Expand Up @@ -13,7 +13,7 @@

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Event\FilterDataEvent;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\Exception\UnexpectedTypeException;

/**
Expand Down Expand Up @@ -50,15 +50,13 @@ public function __construct($allowAdd = false, $allowDelete = false)
static public function getSubscribedEvents()
{
return array(
FormEvents::BIND_NORM_DATA => 'onBindNormData',
FormEvents::BIND => 'onBind',
);
}

public function onBindNormData(FilterDataEvent $event)
public function onBind(FormEvent $event)
{
$dataToMergeInto = $event->getForm()->getNormData();

$form = $event->getForm();
$data = $event->getData();

if (null === $data) {
Expand Down
Expand Up @@ -12,8 +12,7 @@
namespace Symfony\Component\Form\Extension\Core\EventListener;

use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Event\DataEvent;
use Symfony\Component\Form\Event\FilterDataEvent;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand Down Expand Up @@ -67,11 +66,11 @@ static public function getSubscribedEvents()
FormEvents::PRE_SET_DATA => 'preSetData',
FormEvents::PRE_BIND => 'preBind',
// (MergeCollectionListener, MergeDoctrineCollectionListener)
FormEvents::BIND_NORM_DATA => array('onBindNormData', 50),
FormEvents::BIND => array('onBind', 50),
);
}

public function preSetData(DataEvent $event)
public function preSetData(FormEvent $event)
{
$form = $event->getForm();
$data = $event->getData();
Expand All @@ -97,7 +96,7 @@ public function preSetData(DataEvent $event)
}
}

public function preBind(DataEvent $event)
public function preBind(FormEvent $event)
{
$form = $event->getForm();
$data = $event->getData();
Expand Down Expand Up @@ -131,7 +130,7 @@ public function preBind(DataEvent $event)
}
}

public function onBindNormData(FilterDataEvent $event)
public function onBind(FormEvent $event)
{
$form = $event->getForm();
$data = $event->getData();
Expand Down
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\Form\Extension\Core\EventListener;

use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Event\FilterDataEvent;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
Expand All @@ -22,7 +22,7 @@
*/
class TrimListener implements EventSubscriberInterface
{
public function onBindClientData(FilterDataEvent $event)
public function preBind(FormEvent $event)
{
$data = $event->getData();

Expand All @@ -33,6 +33,6 @@ public function onBindClientData(FilterDataEvent $event)

static public function getSubscribedEvents()
{
return array(FormEvents::BIND_CLIENT_DATA => 'onBindClientData');
return array(FormEvents::PRE_BIND => 'preBind');
}
}
Expand Up @@ -14,7 +14,7 @@
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\Event\FilterDataEvent;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;

/**
Expand Down Expand Up @@ -47,7 +47,7 @@ class CsrfValidationListener implements EventSubscriberInterface
static public function getSubscribedEvents()
{
return array(
FormEvents::BIND_CLIENT_DATA => 'onBindClientData',
FormEvents::PRE_BIND => 'preBind',
);
}

Expand All @@ -58,7 +58,7 @@ public function __construct($fieldName, CsrfProviderInterface $csrfProvider, $in
$this->intention = $intention;
}

public function onBindClientData(FilterDataEvent $event)
public function preBind(FormEvent $event)
{
$form = $event->getForm();
$data = $event->getData();
Expand Down

0 comments on commit 33fecca

Please sign in to comment.