Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[FORM] fix post_max_size_message translation #18543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions 2 src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@
<!-- HttpFoundationRequestHandler -->
<service id="form.type_extension.form.request_handler" class="%form.type_extension.form.request_handler.class%" public="false">
<argument type="service" id="form.server_params" />
<argument type="service" id="translator.default" />
<argument>%validator.translation_domain%</argument>
</service>

<service id="form.server_params" class="Symfony\Component\Form\Util\ServerParams" public="false">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Form\RequestHandlerInterface;
use Symfony\Component\Form\Util\ServerParams;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Translation\TranslatorInterface;

/**
* A request processor using the {@link Request} class of the HttpFoundation
Expand All @@ -31,12 +32,24 @@ class HttpFoundationRequestHandler implements RequestHandlerInterface
*/
private $serverParams;

/**
* @var TranslatorInterface
*/
private $translator;

/**
* @var null|string
*/
private $translationDomain;

/**
* {@inheritdoc}
*/
public function __construct(ServerParams $serverParams = null)
public function __construct(ServerParams $serverParams = null, TranslatorInterface $translator = null, $translationDomain = null)
{
$this->serverParams = $serverParams ?: new ServerParams();
$this->translator = $translator;
$this->translationDomain = $translationDomain;
}

/**
Expand Down Expand Up @@ -80,8 +93,14 @@ public function handleRequest(FormInterface $form, $request = null)
// Submit the form, but don't clear the default values
$form->submit(null, false);

$errorMessage = $form->getConfig()->getOption('post_max_size_message');

if (null !== $this->translator) {
$errorMessage = $this->translator->trans($errorMessage, array(), $this->translationDomain);
}

$form->addError(new FormError(
$form->getConfig()->getOption('post_max_size_message'),
$errorMessage,
null,
array('{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize())
));
Expand Down
23 changes: 21 additions & 2 deletions 23 src/Symfony/Component/Form/NativeRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Util\ServerParams;
use Symfony\Component\Translation\TranslatorInterface;

/**
* A request handler using PHP's super globals $_GET, $_POST and $_SERVER.
Expand All @@ -26,12 +27,24 @@ class NativeRequestHandler implements RequestHandlerInterface
*/
private $serverParams;

/**
* @var TranslatorInterface
*/
private $translator;

/**
* @var null|string
*/
private $translationDomain;

/**
* {@inheritdoc}
*/
public function __construct(ServerParams $params = null)
public function __construct(ServerParams $params = null, TranslatorInterface $translator = null, $translationDomain = null)
{
$this->serverParams = $params ?: new ServerParams();
$this->translator = $translator;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should create a translator if null is passed imo, since this can be used with Form as standalone component.

Alright forget it :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a kind of feature, shouldn't be documented in the changelog that you can pass a translator to request handlers ?
Maybe this should go on master ?

$this->translationDomain = $translationDomain;
}

/**
Expand Down Expand Up @@ -88,8 +101,14 @@ public function handleRequest(FormInterface $form, $request = null)
// Submit the form, but don't clear the default values
$form->submit(null, false);

$errorMessage = $form->getConfig()->getOption('post_max_size_message');

if (null !== $this->translator) {
$errorMessage = $this->translator->trans($errorMessage, array(), $this->translationDomain);
}

$form->addError(new FormError(
$form->getConfig()->getOption('post_max_size_message'),
$errorMessage,
null,
array('{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize())
));
Expand Down
34 changes: 33 additions & 1 deletion 34 src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,41 @@ public function getPostMaxSizeFixtures()
);
}

public function testPostMaxSizeExceededTranslation()
{
$this->serverParams->expects($this->once())
->method('getContentLength')
->will($this->returnValue(pow(1024, 3) + 1));

$this->serverParams->expects($this->any())
->method('getNormalizedIniPostMaxSize')
->will($this->returnValue('1G'));

$translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');

$translator->expects($this->any())
->method('trans')
->with($this->equalTo('old max {{ max }}!'))
->willReturn('translated max {{ max }}!');

$this->requestHandler = $this->getRequestHandler($translator);

$options = array('post_max_size_message' => 'old max {{ max }}!');
$form = $this->factory->createNamed('name', 'text', null, $options);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

text --> Symfony\Component\Form\Extension\Core\Type\TextType

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this works only in >=2.8

$this->setRequestData('POST', array(), array());

$this->requestHandler->handleRequest($form, $this->request);

$error = new FormError('translated max {{ max }}!', null, array('{{ max }}' => '1G'));
$error->setOrigin($form);

$this->assertEquals(array($error), iterator_to_array($form->getErrors()));
$this->assertTrue($form->isSubmitted());
}

abstract protected function setRequestData($method, $data, $files = array());

abstract protected function getRequestHandler();
abstract protected function getRequestHandler($translator = null);

abstract protected function getMockFile($suffix = '');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ protected function setRequestData($method, $data, $files = array())
$this->request = Request::create('http://localhost', $method, $data, array(), $files);
}

protected function getRequestHandler()
protected function getRequestHandler($translator = null)
{
return new HttpFoundationRequestHandler($this->serverParams);
return new HttpFoundationRequestHandler($this->serverParams, $translator);
}

protected function getMockFile($suffix = '')
Expand Down
4 changes: 2 additions & 2 deletions 4 src/Symfony/Component/Form/Tests/NativeRequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ protected function setRequestData($method, $data, $files = array())
);
}

protected function getRequestHandler()
protected function getRequestHandler($translator = null)
{
return new NativeRequestHandler($this->serverParams);
return new NativeRequestHandler($this->serverParams, $translator);
}

protected function getMockFile($suffix = '')
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.