[Form] Add a form error if post_max_size has been reached.
This commit is contained in:
parent
e47e4fa56d
commit
47802105d3
@ -12,6 +12,8 @@
|
|||||||
namespace Symfony\Component\Form\Extension\HttpFoundation;
|
namespace Symfony\Component\Form\Extension\HttpFoundation;
|
||||||
|
|
||||||
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
||||||
|
use Symfony\Component\Form\Extension\Validator\Util\ServerParams;
|
||||||
|
use Symfony\Component\Form\FormError;
|
||||||
use Symfony\Component\Form\FormInterface;
|
use Symfony\Component\Form\FormInterface;
|
||||||
use Symfony\Component\Form\RequestHandlerInterface;
|
use Symfony\Component\Form\RequestHandlerInterface;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
@ -24,6 +26,19 @@ use Symfony\Component\HttpFoundation\Request;
|
|||||||
*/
|
*/
|
||||||
class HttpFoundationRequestHandler implements RequestHandlerInterface
|
class HttpFoundationRequestHandler implements RequestHandlerInterface
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var ServerParams
|
||||||
|
*/
|
||||||
|
private $serverParams;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function __construct(ServerParams $params = null)
|
||||||
|
{
|
||||||
|
$this->serverParams = $params ?: new ServerParams();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
@ -61,6 +76,10 @@ class HttpFoundationRequestHandler implements RequestHandlerInterface
|
|||||||
$params = $request->request->get($name, $default);
|
$params = $request->request->get($name, $default);
|
||||||
$files = $request->files->get($name, $default);
|
$files = $request->files->get($name, $default);
|
||||||
} else {
|
} else {
|
||||||
|
if ($this->serverParams->getContentLength() > $this->serverParams->getPostMaxSize()) {
|
||||||
|
$form->addError(new FormError('Max post size exceeded.'));
|
||||||
|
}
|
||||||
|
|
||||||
// Don't submit the form if it is not present in the request
|
// Don't submit the form if it is not present in the request
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
namespace Symfony\Component\Form;
|
namespace Symfony\Component\Form;
|
||||||
|
|
||||||
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
||||||
|
use Symfony\Component\Form\Extension\Validator\Util\ServerParams;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A request handler using PHP's super globals $_GET, $_POST and $_SERVER.
|
* A request handler using PHP's super globals $_GET, $_POST and $_SERVER.
|
||||||
@ -20,6 +21,19 @@ use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
|||||||
*/
|
*/
|
||||||
class NativeRequestHandler implements RequestHandlerInterface
|
class NativeRequestHandler implements RequestHandlerInterface
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var ServerParams
|
||||||
|
*/
|
||||||
|
private $serverParams;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function __construct(ServerParams $params = null)
|
||||||
|
{
|
||||||
|
$this->serverParams = $params ?: new ServerParams();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The allowed keys of the $_FILES array.
|
* The allowed keys of the $_FILES array.
|
||||||
*
|
*
|
||||||
@ -75,6 +89,10 @@ class NativeRequestHandler implements RequestHandlerInterface
|
|||||||
$params = array_key_exists($name, $_POST) ? $_POST[$name] : $default;
|
$params = array_key_exists($name, $_POST) ? $_POST[$name] : $default;
|
||||||
$files = array_key_exists($name, $fixedFiles) ? $fixedFiles[$name] : $default;
|
$files = array_key_exists($name, $fixedFiles) ? $fixedFiles[$name] : $default;
|
||||||
} else {
|
} else {
|
||||||
|
if ($this->serverParams->getContentLength() > $this->serverParams->getPostMaxSize()) {
|
||||||
|
$form->addError(new FormError('Max post size exceeded.'));
|
||||||
|
}
|
||||||
|
|
||||||
// Don't submit the form if it is not present in the request
|
// Don't submit the form if it is not present in the request
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Form\Tests;
|
namespace Symfony\Component\Form\Tests;
|
||||||
|
|
||||||
|
use Symfony\Component\Form\Forms;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
*/
|
*/
|
||||||
@ -21,11 +23,17 @@ abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase
|
|||||||
*/
|
*/
|
||||||
protected $requestHandler;
|
protected $requestHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Symfony\Component\Form\FormFactory
|
||||||
|
*/
|
||||||
|
protected $factory;
|
||||||
|
|
||||||
protected $request;
|
protected $request;
|
||||||
|
|
||||||
protected function setUp()
|
protected function setUp()
|
||||||
{
|
{
|
||||||
$this->requestHandler = $this->getRequestHandler();
|
$this->requestHandler = $this->getRequestHandler();
|
||||||
|
$this->factory = Forms::createFormFactoryBuilder()->getFormFactory();
|
||||||
$this->request = null;
|
$this->request = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -249,6 +257,19 @@ abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->requestHandler->handleRequest($form, $this->request);
|
$this->requestHandler->handleRequest($form, $this->request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testAddFormErrorIfPostMaxSizeExceeded()
|
||||||
|
{
|
||||||
|
$form = $this->factory->createNamed('name', 'text');
|
||||||
|
$this->setRequestData('POST', array(), array());
|
||||||
|
$_SERVER['CONTENT_LENGTH'] = 1000000000;
|
||||||
|
|
||||||
|
$this->requestHandler->handleRequest($form, $this->request);
|
||||||
|
|
||||||
|
$this->assertEquals("ERROR: Max post size exceeded.\n", $form->getErrorsAsString());
|
||||||
|
|
||||||
|
unset($_SERVER['CONTENT_LENGTH']);
|
||||||
|
}
|
||||||
|
|
||||||
abstract protected function setRequestData($method, $data, $files = array());
|
abstract protected function setRequestData($method, $data, $files = array());
|
||||||
|
|
||||||
abstract protected function getRequestHandler();
|
abstract protected function getRequestHandler();
|
||||||
|
Reference in New Issue
Block a user