Skip to content

Commit

Permalink
bug #30961 [Form] fix translating file validation error message (xabbuh)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Apr 7, 2019
2 parents 62e5a91 + 9c41842 commit c82e2df
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Expand Up @@ -93,7 +93,7 @@
<deprecated>The "%service_id%" service is deprecated since Symfony 3.1 and will be removed in 4.0.</deprecated>
</service>
<service id="form.type.file" class="Symfony\Component\Form\Extension\Core\Type\FileType" public="true">
<deprecated>The "%service_id%" service is deprecated since Symfony 3.1 and will be removed in 4.0.</deprecated>
<argument type="service" id="translator" on-invalid="ignore" />
</service>
<service id="form.type.hidden" class="Symfony\Component\Form\Extension\Core\Type\HiddenType" public="true">
<deprecated>The "%service_id%" service is deprecated since Symfony 3.1 and will be removed in 4.0.</deprecated>
Expand Down
16 changes: 15 additions & 1 deletion src/Symfony/Component/Form/Extension/Core/Type/FileType.php
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;

class FileType extends AbstractType
{
Expand All @@ -32,6 +33,13 @@ class FileType extends AbstractType
self::MIB_BYTES => 'MiB',
];

private $translator;

public function __construct(TranslatorInterface $translator = null)
{
$this->translator = $translator;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -150,7 +158,13 @@ private function getFileUploadError($errorCode)
$messageTemplate = 'The file could not be uploaded.';
}

return new FormError($messageTemplate, $messageTemplate, $messageParameters);
if (null !== $this->translator) {
$message = $this->translator->trans($messageTemplate, $messageParameters);
} else {
$message = strtr($messageTemplate, $messageParameters);
}

return new FormError($message, $messageTemplate, $messageParameters);
}

/**
Expand Down

0 comments on commit c82e2df

Please sign in to comment.