Skip to content

Commit 9d8a5e5

Browse files
committed
fix post_max_size_message translation
1 parent 910eff0 commit 9d8a5e5

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,10 @@
189189
<service id="form.type_extension.submit.validator" class="Symfony\Component\Form\Extension\Validator\Type\SubmitTypeValidatorExtension">
190190
<tag name="form.type_extension" alias="submit" />
191191
</service>
192+
<service id="form.type_extension.upload.validator" class="Symfony\Component\Form\Extension\Validator\Type\UploadValidatorExtension">
193+
<tag name="form.type_extension" alias="form" />
194+
<argument type="service" id="translator"/>
195+
<argument type="string">%validator.translation_domain%</argument>
196+
</service>
192197
</services>
193198
</container>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Extension\Validator\Type;
13+
14+
use Symfony\Component\Form\AbstractTypeExtension;
15+
use Symfony\Component\OptionsResolver\Options;
16+
use Symfony\Component\OptionsResolver\OptionsResolver;
17+
use Symfony\Component\Translation\TranslatorInterface;
18+
19+
/**
20+
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
21+
* @author David Badura <d.a.badura@gmail.com>
22+
*/
23+
class UploadValidatorExtension extends AbstractTypeExtension
24+
{
25+
private $translator;
26+
private $translationDomain;
27+
28+
/**
29+
* @param TranslatorInterface $translator The translator for translating error messages
30+
* @param null|string $translationDomain The translation domain for translating
31+
*/
32+
public function __construct(TranslatorInterface $translator, $translationDomain = null)
33+
{
34+
$this->translator = $translator;
35+
$this->translationDomain = $translationDomain;
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function configureOptions(OptionsResolver $resolver)
42+
{
43+
$translator = $this->translator;
44+
$translationDomain = $this->translationDomain;
45+
46+
$resolver->setNormalizer('post_max_size_message', function (Options $options, $errorMessage) use ($translator, $translationDomain) {
47+
return $translator->trans($errorMessage, array(), $translationDomain);
48+
});
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
public function getExtendedType()
55+
{
56+
return 'form';
57+
}
58+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Tests\Extension\Validator\Type;
13+
14+
use Symfony\Component\Form\Extension\Validator\Type\UploadValidatorExtension;
15+
use Symfony\Component\OptionsResolver\OptionsResolver;
16+
17+
class UploadValidatorExtensionTest extends TypeTestCase
18+
{
19+
public function testPostMaxSizeTranslation()
20+
{
21+
$translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
22+
23+
$translator->expects($this->any())
24+
->method('trans')
25+
->with($this->equalTo('old max {{ max }}!'))
26+
->willReturn('translated max {{ max }}!');
27+
28+
$extension = new UploadValidatorExtension($translator);
29+
30+
$resolver = new OptionsResolver();
31+
$resolver->setDefault('post_max_size_message', 'old max {{ max }}!');
32+
33+
$extension->configureOptions($resolver);
34+
$options = $resolver->resolve();
35+
36+
$this->assertEquals('translated max {{ max }}!', $options['post_max_size_message']);
37+
}
38+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy