[Form] Removed depracted events PRE_BIND, BIND and POST_BIND
This commit is contained in:
parent
afcf52f485
commit
1cdb655b11
@ -1,28 +0,0 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Form\Deprecated;
|
||||
|
||||
trigger_error('Constants PRE_BIND, BIND and POST_BIND on class Symfony\Component\Form\FormEvents were deprecated in Symfony 2.3 and will be removed in 3.0. Use PRE_SUBMIT, SUBMIT and POST_SUBMIT instead.', E_USER_DEPRECATED);
|
||||
|
||||
/**
|
||||
* @deprecated since 2.7, to be removed in 3.0.
|
||||
* @internal
|
||||
*/
|
||||
final class FormEvents
|
||||
{
|
||||
const PRE_BIND = 'form.pre_bind';
|
||||
const BIND = 'form.bind';
|
||||
const POST_BIND = 'form.post_bind';
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
}
|
@ -1,84 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Form\Extension\HttpFoundation\EventListener;
|
||||
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*
|
||||
* @deprecated Deprecated since version 2.3, to be removed in 3.0. Pass the
|
||||
* Request instance to {@link Form::handleRequest()} instead.
|
||||
*/
|
||||
class BindRequestListener implements EventSubscriberInterface
|
||||
{
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
// High priority in order to supersede other listeners
|
||||
return array(FormEvents::PRE_SUBMIT => array('preBind', 128));
|
||||
}
|
||||
|
||||
public function preBind(FormEvent $event)
|
||||
{
|
||||
$form = $event->getForm();
|
||||
|
||||
/* @var Request $request */
|
||||
$request = $event->getData();
|
||||
|
||||
// Only proceed if we actually deal with a Request
|
||||
if (!$request instanceof Request) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Uncomment this as soon as the deprecation note should be shown
|
||||
// trigger_error('Passing a Request instance to Form::submit() is deprecated since version 2.3 and will be disabled in 3.0. Call Form::process($request) instead.', E_USER_DEPRECATED);
|
||||
|
||||
$name = $form->getConfig()->getName();
|
||||
$default = $form->getConfig()->getCompound() ? array() : null;
|
||||
|
||||
// For request methods that must not have a request body we fetch data
|
||||
// from the query string. Otherwise we look for data in the request body.
|
||||
switch ($request->getMethod()) {
|
||||
case 'GET':
|
||||
case 'HEAD':
|
||||
case 'TRACE':
|
||||
$data = '' === $name
|
||||
? $request->query->all()
|
||||
: $request->query->get($name, $default);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
if ('' === $name) {
|
||||
// Form bound without name
|
||||
$params = $request->request->all();
|
||||
$files = $request->files->all();
|
||||
} else {
|
||||
$params = $request->request->get($name, $default);
|
||||
$files = $request->files->get($name, $default);
|
||||
}
|
||||
|
||||
if (is_array($params) && is_array($files)) {
|
||||
$data = array_replace_recursive($params, $files);
|
||||
} else {
|
||||
$data = $params ?: $files;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
$event->setData($data);
|
||||
}
|
||||
}
|
@ -12,7 +12,6 @@
|
||||
namespace Symfony\Component\Form\Extension\HttpFoundation\Type;
|
||||
|
||||
use Symfony\Component\Form\AbstractTypeExtension;
|
||||
use Symfony\Component\Form\Extension\HttpFoundation\EventListener\BindRequestListener;
|
||||
use Symfony\Component\Form\RequestHandlerInterface;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler;
|
||||
@ -22,11 +21,6 @@ use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler
|
||||
*/
|
||||
class FormTypeHttpFoundationExtension extends AbstractTypeExtension
|
||||
{
|
||||
/**
|
||||
* @var BindRequestListener
|
||||
*/
|
||||
private $listener;
|
||||
|
||||
/**
|
||||
* @var RequestHandlerInterface
|
||||
*/
|
||||
@ -37,7 +31,6 @@ class FormTypeHttpFoundationExtension extends AbstractTypeExtension
|
||||
*/
|
||||
public function __construct(RequestHandlerInterface $requestHandler = null)
|
||||
{
|
||||
$this->listener = new BindRequestListener();
|
||||
$this->requestHandler = $requestHandler ?: new HttpFoundationRequestHandler();
|
||||
}
|
||||
|
||||
@ -46,7 +39,6 @@ class FormTypeHttpFoundationExtension extends AbstractTypeExtension
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder->addEventSubscriber($this->listener);
|
||||
$builder->setRequestHandler($this->requestHandler);
|
||||
}
|
||||
|
||||
|
@ -10,8 +10,6 @@
|
||||
|
||||
namespace Symfony\Component\Form;
|
||||
|
||||
use Symfony\Component\Form\Deprecated\FormEvents as Deprecated;
|
||||
|
||||
/**
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*/
|
||||
@ -73,30 +71,6 @@ final class FormEvents
|
||||
*/
|
||||
const POST_SET_DATA = 'form.post_set_data';
|
||||
|
||||
/**
|
||||
* @deprecated Deprecated since version 2.3, to be removed in 3.0. Use
|
||||
* {@link PRE_SUBMIT} instead.
|
||||
*
|
||||
* @Event
|
||||
*/
|
||||
const PRE_BIND = Deprecated::PRE_BIND;
|
||||
|
||||
/**
|
||||
* @deprecated Deprecated since version 2.3, to be removed in 3.0. Use
|
||||
* {@link SUBMIT} instead.
|
||||
*
|
||||
* @Event
|
||||
*/
|
||||
const BIND = Deprecated::BIND;
|
||||
|
||||
/**
|
||||
* @deprecated Deprecated since version 2.3, to be removed in 3.0. Use
|
||||
* {@link POST_SUBMIT} instead.
|
||||
*
|
||||
* @Event
|
||||
*/
|
||||
const POST_BIND = Deprecated::POST_BIND;
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
|
@ -1,255 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Form\Tests\Extension\HttpFoundation\EventListener;
|
||||
|
||||
use Symfony\Component\Form\Extension\HttpFoundation\EventListener\BindRequestListener;
|
||||
use Symfony\Component\Form\Form;
|
||||
use Symfony\Component\Form\FormConfigBuilder;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
|
||||
/**
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*/
|
||||
class LegacyBindRequestListenerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
private $values;
|
||||
|
||||
private $filesPlain;
|
||||
|
||||
private $filesNested;
|
||||
|
||||
/**
|
||||
* @var UploadedFile
|
||||
*/
|
||||
private $uploadedFile;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
|
||||
|
||||
$path = tempnam(sys_get_temp_dir(), 'sf2');
|
||||
touch($path);
|
||||
|
||||
$this->values = array(
|
||||
'name' => 'Bernhard',
|
||||
'image' => array('filename' => 'foobar.png'),
|
||||
);
|
||||
|
||||
$this->filesPlain = array(
|
||||
'image' => array(
|
||||
'error' => UPLOAD_ERR_OK,
|
||||
'name' => 'upload.png',
|
||||
'size' => 123,
|
||||
'tmp_name' => $path,
|
||||
'type' => 'image/png',
|
||||
),
|
||||
);
|
||||
|
||||
$this->filesNested = array(
|
||||
'error' => array('image' => UPLOAD_ERR_OK),
|
||||
'name' => array('image' => 'upload.png'),
|
||||
'size' => array('image' => 123),
|
||||
'tmp_name' => array('image' => $path),
|
||||
'type' => array('image' => 'image/png'),
|
||||
);
|
||||
|
||||
$this->uploadedFile = new UploadedFile($path, 'upload.png', 'image/png', 123, UPLOAD_ERR_OK);
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
unlink($this->uploadedFile->getRealPath());
|
||||
}
|
||||
|
||||
public function requestMethodProvider()
|
||||
{
|
||||
return array(
|
||||
array('POST'),
|
||||
array('PUT'),
|
||||
array('DELETE'),
|
||||
array('PATCH'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider requestMethodProvider
|
||||
*/
|
||||
public function testSubmitRequest($method)
|
||||
{
|
||||
$values = array('author' => $this->values);
|
||||
$files = array('author' => $this->filesNested);
|
||||
$request = new Request(array(), $values, array(), array(), $files, array(
|
||||
'REQUEST_METHOD' => $method,
|
||||
));
|
||||
|
||||
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
||||
$config = new FormConfigBuilder('author', null, $dispatcher);
|
||||
$form = new Form($config);
|
||||
$event = new FormEvent($form, $request);
|
||||
|
||||
$listener = new BindRequestListener();
|
||||
$listener->preBind($event);
|
||||
|
||||
$this->assertEquals(array(
|
||||
'name' => 'Bernhard',
|
||||
'image' => $this->uploadedFile,
|
||||
), $event->getData());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider requestMethodProvider
|
||||
*/
|
||||
public function testSubmitRequestWithEmptyName($method)
|
||||
{
|
||||
$request = new Request(array(), $this->values, array(), array(), $this->filesPlain, array(
|
||||
'REQUEST_METHOD' => $method,
|
||||
));
|
||||
|
||||
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
||||
$config = new FormConfigBuilder('', null, $dispatcher);
|
||||
$form = new Form($config);
|
||||
$event = new FormEvent($form, $request);
|
||||
|
||||
$listener = new BindRequestListener();
|
||||
$listener->preBind($event);
|
||||
|
||||
$this->assertEquals(array(
|
||||
'name' => 'Bernhard',
|
||||
'image' => $this->uploadedFile,
|
||||
), $event->getData());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider requestMethodProvider
|
||||
*/
|
||||
public function testSubmitEmptyRequestToCompoundForm($method)
|
||||
{
|
||||
$request = new Request(array(), array(), array(), array(), array(), array(
|
||||
'REQUEST_METHOD' => $method,
|
||||
));
|
||||
|
||||
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
||||
$config = new FormConfigBuilder('author', null, $dispatcher);
|
||||
$config->setCompound(true);
|
||||
$config->setDataMapper($this->getMock('Symfony\Component\Form\DataMapperInterface'));
|
||||
$form = new Form($config);
|
||||
$event = new FormEvent($form, $request);
|
||||
|
||||
$listener = new BindRequestListener();
|
||||
$listener->preBind($event);
|
||||
|
||||
// Default to empty array
|
||||
$this->assertEquals(array(), $event->getData());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider requestMethodProvider
|
||||
*/
|
||||
public function testSubmitEmptyRequestToSimpleForm($method)
|
||||
{
|
||||
$request = new Request(array(), array(), array(), array(), array(), array(
|
||||
'REQUEST_METHOD' => $method,
|
||||
));
|
||||
|
||||
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
||||
$config = new FormConfigBuilder('author', null, $dispatcher);
|
||||
$config->setCompound(false);
|
||||
$form = new Form($config);
|
||||
$event = new FormEvent($form, $request);
|
||||
|
||||
$listener = new BindRequestListener();
|
||||
$listener->preBind($event);
|
||||
|
||||
// Default to null
|
||||
$this->assertNull($event->getData());
|
||||
}
|
||||
|
||||
public function testSubmitGetRequest()
|
||||
{
|
||||
$values = array('author' => $this->values);
|
||||
$request = new Request($values, array(), array(), array(), array(), array(
|
||||
'REQUEST_METHOD' => 'GET',
|
||||
));
|
||||
|
||||
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
||||
$config = new FormConfigBuilder('author', null, $dispatcher);
|
||||
$form = new Form($config);
|
||||
$event = new FormEvent($form, $request);
|
||||
|
||||
$listener = new BindRequestListener();
|
||||
$listener->preBind($event);
|
||||
|
||||
$this->assertEquals(array(
|
||||
'name' => 'Bernhard',
|
||||
'image' => array('filename' => 'foobar.png'),
|
||||
), $event->getData());
|
||||
}
|
||||
|
||||
public function testSubmitGetRequestWithEmptyName()
|
||||
{
|
||||
$request = new Request($this->values, array(), array(), array(), array(), array(
|
||||
'REQUEST_METHOD' => 'GET',
|
||||
));
|
||||
|
||||
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
||||
$config = new FormConfigBuilder('', null, $dispatcher);
|
||||
$form = new Form($config);
|
||||
$event = new FormEvent($form, $request);
|
||||
|
||||
$listener = new BindRequestListener();
|
||||
$listener->preBind($event);
|
||||
|
||||
$this->assertEquals(array(
|
||||
'name' => 'Bernhard',
|
||||
'image' => array('filename' => 'foobar.png'),
|
||||
), $event->getData());
|
||||
}
|
||||
|
||||
public function testSubmitEmptyGetRequestToCompoundForm()
|
||||
{
|
||||
$request = new Request(array(), array(), array(), array(), array(), array(
|
||||
'REQUEST_METHOD' => 'GET',
|
||||
));
|
||||
|
||||
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
||||
$config = new FormConfigBuilder('author', null, $dispatcher);
|
||||
$config->setCompound(true);
|
||||
$config->setDataMapper($this->getMock('Symfony\Component\Form\DataMapperInterface'));
|
||||
$form = new Form($config);
|
||||
$event = new FormEvent($form, $request);
|
||||
|
||||
$listener = new BindRequestListener();
|
||||
$listener->preBind($event);
|
||||
|
||||
$this->assertEquals(array(), $event->getData());
|
||||
}
|
||||
|
||||
public function testSubmitEmptyGetRequestToSimpleForm()
|
||||
{
|
||||
$request = new Request(array(), array(), array(), array(), array(), array(
|
||||
'REQUEST_METHOD' => 'GET',
|
||||
));
|
||||
|
||||
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
||||
$config = new FormConfigBuilder('author', null, $dispatcher);
|
||||
$config->setCompound(false);
|
||||
$form = new Form($config);
|
||||
$event = new FormEvent($form, $request);
|
||||
|
||||
$listener = new BindRequestListener();
|
||||
$listener->preBind($event);
|
||||
|
||||
$this->assertNull($event->getData());
|
||||
}
|
||||
}
|
@ -122,19 +122,19 @@ class SimpleFormTest extends AbstractFormTest
|
||||
public function testFalseIsConvertedToNull()
|
||||
{
|
||||
$mock = $this->getMockBuilder('\stdClass')
|
||||
->setMethods(array('preBind'))
|
||||
->setMethods(array('preSubmit'))
|
||||
->getMock();
|
||||
$mock->expects($this->once())
|
||||
->method('preBind')
|
||||
->method('preSubmit')
|
||||
->with($this->callback(function ($event) {
|
||||
return null === $event->getData();
|
||||
}));
|
||||
|
||||
$config = new FormConfigBuilder('name', null, $this->dispatcher);
|
||||
$config->addEventListener(FormEvents::PRE_SUBMIT, array($mock, 'preBind'));
|
||||
$config->addEventListener(FormEvents::PRE_SUBMIT, array($mock, 'preSubmit'));
|
||||
$form = new Form($config);
|
||||
|
||||
$form->submit(false);
|
||||
$form->bind(false);
|
||||
|
||||
$this->assertTrue($form->isValid());
|
||||
$this->assertNull($form->getData());
|
||||
|
Reference in New Issue
Block a user