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

Skip to content

[Form] fix translating file validation error message #30961

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
Apr 7, 2019
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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