zendframework/zend-validator

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:

const INI_SIZE = 'fileUploadFileErrorIniSize';
const FORM_SIZE = 'fileUploadFileErrorFormSize';
const PARTIAL = 'fileUploadFileErrorPartial';
const NO_FILE = 'fileUploadFileErrorNoFile';
const NO_TMP_DIR = 'fileUploadFileErrorNoTmpDir';
const CANT_WRITE = 'fileUploadFileErrorCantWrite';
const EXTENSION = 'fileUploadFileErrorExtension';
const ATTACK = 'fileUploadFileErrorAttack';
const FILE_NOT_FOUND = 'fileUploadFileErrorFileNotFound';
const UNKNOWN = 'fileUploadFileErrorUnknown';

protected $messageTemplates = [
self::INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
self::FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was '
. 'specified in the HTML form',
self::PARTIAL => 'The uploaded file was only partially uploaded',
self::NO_FILE => 'No file was uploaded',
self::NO_TMP_DIR => 'Missing a temporary folder',
self::CANT_WRITE => 'Failed to write file to disk',
self::EXTENSION => 'A PHP extension stopped the file upload',
self::ATTACK => 'File was illegally uploaded. This could be a possible attack',
self::FILE_NOT_FOUND => 'File was not found',
self::UNKNOWN => 'Unknown error while uploading file',
];

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
MyForm

@MarshMalloW0007

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!