Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c82e2df

Browse files
committed
bug #30961 [Form] fix translating file validation error message (xabbuh)
This PR was merged into the 3.4 branch. Discussion ---------- [Form] fix translating file validation error message | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | I messed this up in #30895. #FOSSHackathons #EUFOSSA Commits ------- 9c41842 fix translating file validation error message
2 parents 62e5a91 + 9c41842 commit c82e2df

File tree

2 files changed

+16
-2
lines changed
  • src/Symfony

2 files changed

+16
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
<deprecated>The "%service_id%" service is deprecated since Symfony 3.1 and will be removed in 4.0.</deprecated>
9494
</service>
9595
<service id="form.type.file" class="Symfony\Component\Form\Extension\Core\Type\FileType" public="true">
96-
<deprecated>The "%service_id%" service is deprecated since Symfony 3.1 and will be removed in 4.0.</deprecated>
96+
<argument type="service" id="translator" on-invalid="ignore" />
9797
</service>
9898
<service id="form.type.hidden" class="Symfony\Component\Form\Extension\Core\Type\HiddenType" public="true">
9999
<deprecated>The "%service_id%" service is deprecated since Symfony 3.1 and will be removed in 4.0.</deprecated>

src/Symfony/Component/Form/Extension/Core/Type/FileType.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Form\FormView;
2121
use Symfony\Component\OptionsResolver\Options;
2222
use Symfony\Component\OptionsResolver\OptionsResolver;
23+
use Symfony\Component\Translation\TranslatorInterface;
2324

2425
class FileType extends AbstractType
2526
{
@@ -32,6 +33,13 @@ class FileType extends AbstractType
3233
self::MIB_BYTES => 'MiB',
3334
];
3435

36+
private $translator;
37+
38+
public function __construct(TranslatorInterface $translator = null)
39+
{
40+
$this->translator = $translator;
41+
}
42+
3543
/**
3644
* {@inheritdoc}
3745
*/
@@ -150,7 +158,13 @@ private function getFileUploadError($errorCode)
150158
$messageTemplate = 'The file could not be uploaded.';
151159
}
152160

153-
return new FormError($messageTemplate, $messageTemplate, $messageParameters);
161+
if (null !== $this->translator) {
162+
$message = $this->translator->trans($messageTemplate, $messageParameters);
163+
} else {
164+
$message = strtr($messageTemplate, $messageParameters);
165+
}
166+
167+
return new FormError($message, $messageTemplate, $messageParameters);
154168
}
155169

156170
/**

0 commit comments

Comments
 (0)