Can not translate messages from the FileUploadFile validator.
Closed this issue · 4 comments
use Zend\Validator\File\UploadFile;
$inputFilter->add([
'name' => 'IMAGE_INFRACTION',
'type' => 'Zend\InputFilter\FileInput',
'required' => true,
'filters' => [
['name' => 'StripTags'],
['name' => 'StringTrim'],
['name' => 'FileRenameUpload',
'options' => [
'target' => './data/upload',
'useUploadName' => true,
'useUploadExtension' => true,
'overwrite' => true,
'randomize' => false,
]
]
],
'validators' => [
[
'name' => 'FileUploadFile'
'options' => [
'messages' => [
UploadFile::INVALID => 'xxx.',
UploadFile::IS_EMPTY => 'yyy.',
],
],
],
],
]);
Does not work and always gives the message "No file was uploaded"
I modified my code to have a general translation of my module:
module.php
public function onBootstrap(MvcEvent $event)
{
...
$eventManager = $event->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$i18nTranslator = new I18nTranslator();
$i18nTranslator->setLocale('fr_FR');
$translator = new Translator($i18nTranslator);
$translator->addTranslationFile('phpArray', 'vendor/zendframework/zend-i18n-resources/languages/fr/Zend_Validate.php', 'default', 'fr_FR');
AbstractValidator::setDefaultTranslator($translator);
}
Does not work and always gives the message "No file was uploaded".
For my other validators, everything works (I have not yet tested all the validators ... :-)
Best regards
@MarshMalloW0007
The current result is correct because the constants UploadFile::INVALID
and UploadFile::IS_EMPTY
do not exist.
See:
zend-validator/src/File/UploadFile.php
Lines 24 to 33 in e633fe6
zend-validator/src/File/UploadFile.php
Lines 38 to 50 in e633fe6
Hello Froschdesign,
That's right.
At first, I tried to modify the messages in my Form file but only the message 'No file was uploaded' remained in English.
Then I modified my Module.php to include a general translation. Again, everything is translated except 'No file was uploaded'.
For the issue, I tried quickly to recreate my initial Form, with the error you raised :-)
I attach a screenshot of my web form and my Module.php
FYI, I'm using ZF3.
Module.php.txt
At first, I tried to modify the messages in my Form…
This means you must use the existing constants of the class Zend\Validator\File\Upload
and not any constants.
Again, everything is translated except 'No file was uploaded'.
I think this is a problem in the translation files of zend-i18n-resources because the message is different between the validator and the translation file.
As workaround you can set the message with the correct constants for your input-filter.
To solve the problem, we must write a script to check all message identifiers in the translation files. This must be done at https://github.com/zendframework/zend-i18n-resources
Thanks for reporting!
Related to zendframework/zend-i18n-resources#43