bug #19595 [form] lazy trans post_max_size_message
. (aitboudad)
This PR was merged into the 2.7 branch.
Discussion
----------
[form] lazy trans `post_max_size_message`.
| Q | A
| ------------- | ---
| Branch? | 2.7
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #19256
| License | MIT
| Doc PR | ~
Commits
-------
c03164e
[form] lazy trans `post_max_size_message`.
This commit is contained in:
commit
3b5c353861
@ -146,6 +146,13 @@ class FormType extends BaseType
|
||||
};
|
||||
};
|
||||
|
||||
// Wrap "post_max_size_message" in a closure to translate it lazily
|
||||
$uploadMaxSizeMessage = function (Options $options) {
|
||||
return function () use ($options) {
|
||||
return $options['post_max_size_message'];
|
||||
};
|
||||
};
|
||||
|
||||
// For any form that is not represented by a single HTML control,
|
||||
// errors should bubble up by default
|
||||
$errorBubbling = function (Options $options) {
|
||||
@ -207,9 +214,11 @@ class FormType extends BaseType
|
||||
'action' => '',
|
||||
'attr' => $defaultAttr,
|
||||
'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.',
|
||||
'upload_max_size_message' => $uploadMaxSizeMessage, // internal
|
||||
));
|
||||
|
||||
$resolver->setAllowedTypes('label_attr', 'array');
|
||||
$resolver->setAllowedTypes('upload_max_size_message', array('callable'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -78,7 +78,7 @@ class HttpFoundationRequestHandler implements RequestHandlerInterface
|
||||
$form->submit(null, false);
|
||||
|
||||
$form->addError(new FormError(
|
||||
$form->getConfig()->getOption('post_max_size_message'),
|
||||
call_user_func($form->getConfig()->getOption('upload_max_size_message')),
|
||||
null,
|
||||
array('{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize())
|
||||
));
|
||||
|
@ -42,9 +42,10 @@ class UploadValidatorExtension extends AbstractTypeExtension
|
||||
{
|
||||
$translator = $this->translator;
|
||||
$translationDomain = $this->translationDomain;
|
||||
|
||||
$resolver->setNormalizer('post_max_size_message', function (Options $options, $errorMessage) use ($translator, $translationDomain) {
|
||||
return $translator->trans($errorMessage, array(), $translationDomain);
|
||||
$resolver->setNormalizer('upload_max_size_message', function (Options $options, $message) use ($translator, $translationDomain) {
|
||||
return function () use ($translator, $translationDomain, $message) {
|
||||
return $translator->trans(call_user_func($message), array(), $translationDomain);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -86,7 +86,7 @@ class NativeRequestHandler implements RequestHandlerInterface
|
||||
$form->submit(null, false);
|
||||
|
||||
$form->addError(new FormError(
|
||||
$form->getConfig()->getOption('post_max_size_message'),
|
||||
call_user_func($form->getConfig()->getOption('upload_max_size_message')),
|
||||
null,
|
||||
array('{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize())
|
||||
));
|
||||
|
@ -13,6 +13,7 @@ namespace Symfony\Component\Form\Tests\Extension\Validator\Type;
|
||||
|
||||
use Symfony\Component\Form\Extension\Validator\Type\UploadValidatorExtension;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\OptionsResolver\Options;
|
||||
|
||||
class UploadValidatorExtensionTest extends TypeTestCase
|
||||
{
|
||||
@ -29,10 +30,15 @@ class UploadValidatorExtensionTest extends TypeTestCase
|
||||
|
||||
$resolver = new OptionsResolver();
|
||||
$resolver->setDefault('post_max_size_message', 'old max {{ max }}!');
|
||||
$resolver->setDefault('upload_max_size_message', function (Options $options, $message) {
|
||||
return function () use ($options) {
|
||||
return $options['post_max_size_message'];
|
||||
};
|
||||
});
|
||||
|
||||
$extension->configureOptions($resolver);
|
||||
$options = $resolver->resolve();
|
||||
|
||||
$this->assertEquals('translated max {{ max }}!', $options['post_max_size_message']);
|
||||
$this->assertEquals('translated max {{ max }}!', call_user_func($options['upload_max_size_message']));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user