10000 [Form] Add a form error if post_max_size has been reached. · symfony/symfony@4780210 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4780210

Browse files
rpg600webmozart
authored andcommitted
[Form] Add a form error if post_max_size has been reached.
1 parent e47e4fa commit 4780210

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Form\Extension\HttpFoundation;
1313

1414
use Symfony\Component\Form\Exception\UnexpectedTypeException;
15+
use Symfony\Component\Form\Extension\Validator\Util\ServerParams;
16+
use Symfony\Component\Form\FormError;
1517
use Symfony\Component\Form\FormInterface;
1618
use Symfony\Component\Form\RequestHandlerInterface;
1719
use Symfony\Component\HttpFoundation\Request;
@@ -24,6 +26,19 @@
2426
*/
2527
class HttpFoundationRequestHandler implements RequestHandlerInterface
2628
{
29+
/**
30+
* @var ServerParams
31+
*/
32+
private $serverParams;
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function __construct(ServerParams $params = null)
38+
{
39+
$this->serverParams = $params ?: new ServerParams();
40+
}
41+
2742
/**
2843
* {@inheritdoc}
2944
*/
@@ -61,6 +76,10 @@ public function handleRequest(FormInterface $form, $request = null)
6176
$params = $request->request->get($name, $default);
6277
$files = $request->files->get($name, $default);
6378
} else {
79+
if ($this->serverParams->getContentLength() > $this->serverParams->getPostMaxSize()) {
80+
$form->addError(new FormError('Max post size exceeded.'));
81+
}
82+
6483
// Don't submit the form if it is not present in the request
6584
return;
6685
}

src/Symfony/Component/Form/NativeRequestHandler.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form;
1313

1414
use Symfony\Component\Form\Exception\UnexpectedTypeException;
15+
use Symfony\Component\Form\Extension\Validator\Util\ServerParams;
1516

1617
/**
1718
* A request handler using PHP's super globals $_GET, $_POST and $_SERVER.
@@ -20,6 +21,19 @@
2021
*/
2122
class NativeRequestHandler implements RequestHandlerInterface
2223
{
24+
/**
25+
* @var ServerParams
26+
*/
27+
private $serverParams;
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function __construct(ServerParams $params = null)
33+
{
34+
$this->serverParams = $params ?: new ServerParams();
35+
}
36+
2337
/**
2438
* The allowed keys of the $_FILES array.
2539
*
@@ -75,6 +89,10 @@ public function handleRequest(FormInterface $form, $request = null)
7589
$params = array_key_exists($name, $_POST) ? $_POST[$name] : $default;
7690
$files = array_key_exists($name, $fixedFiles) ? $fixedFiles[$name] : $default;
7791
} else {
92+
if ($this->serverParams->getContentLength() > $this->serverParams->getPostMaxSize()) {
93+
$form->addError(new FormError('Max post size exceeded.'));
94+
}
95+
7896
// Don't submit the form if it is not present in the request
7997
return;
8098
}

src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Form\Tests;
1313

14+
use Symfony\Component\Form\Forms;
15+
1416
/**
1517
* @author Bernhard Schussek <bschussek@gmail.com>
1618
*/
@@ -21,11 +23,17 @@ abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase
2123
*/
2224
protected $requestHandler;
2325

26+
/**
27+
* @var \Symfony\Component\Form\FormFactory
28+
*/
29+
protected $factory;
30+
2431
protected $request;
2532

2633
protected function setUp()
2734
{
2835
$this->requestHandler = $this->getRequestHandler();
36+
$this->factory = Forms::createFormFactoryBuilder()->getFormFactory();
2937
$this->request = null;
3038
}
3139

@@ -249,6 +257,19 @@ public function testSubmitFileIfNoParam($method)
249257
$this->requestHandler->handleRequest($form, $this->request);
250258
}
251259

260+
public function testAddFormErrorIfPostMaxSizeExceeded()
261+
{
262+
$form = $this->factory->createNamed('name', 'text');
263+
$this->setRequestData('POST', array(), array());
264+
$_SERVER['CONTENT_LENGTH'] = 1000000000;
265+
266+
$this->requestHandler->handleRequest($form, $this->request);
267+
268+
$this->assertEquals("ERROR: Max post size exceeded.\n", $form->getErrorsAsString());
269+
270+
unset($_SERVER['CONTENT_LENGTH']);
271+
}
272+
252273
abstract protected function setRequestData($method, $data, $files = array());
253274

254275
abstract protected function getRequestHandler();

0 commit comments

Comments
 (0)
0