8000 [form] lazy trans `post_max_size_message`. by aitboudad · Pull Request #19595 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[form] lazy trans post_max_size_message. #19595

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

Merged
merged 1 commit into from
Sep 6, 2016
Merged
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
9 changes: 9 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/FormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ public function configureOptions(OptionsResolver $resolver)
};
};

// 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) {
Expand Down Expand Up @@ -207,9 +214,11 @@ public function configureOptions(OptionsResolver $resolver)
'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'));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function handleRequest(FormInterface $form, $request = null)
$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())
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ public function configureOptions(OptionsResolver $resolver)
{
$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);
};
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/NativeRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function handleRequest(FormInterface $form, $request = null)
$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())
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Form\Extension\Validator\Type\UploadValidatorExtension;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\Options;

class UploadValidatorExtensionTest extends TypeTestCase
{
Expand All @@ -29,10 +30,15 @@ public function testPostMaxSizeTranslation()

$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']));
}
}
0