zendframework / zend-validator

Validator component from Zend Framework
BSD 3-Clause "New" or "Revised" License
181 stars 136 forks source link

Can not translate messages from the FileUploadFile validator. #267

Closed MarshMalloW0007 closed 5 years ago

MarshMalloW0007 commented 5 years ago
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

froschdesign commented 5 years ago

@MarshMalloW0007 The current result is correct because the constants UploadFile::INVALID and UploadFile::IS_EMPTY do not exist.

See: https://github.com/zendframework/zend-validator/blob/e633fe68f6dbfafe201bb6f10ea29657992b7b4e/src/File/UploadFile.php#L24-L33

https://github.com/zendframework/zend-validator/blob/e633fe68f6dbfafe201bb6f10ea29657992b7b4e/src/File/UploadFile.php#L38-L50

MarshMalloW0007 commented 5 years ago

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

froschdesign commented 5 years ago

@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!

froschdesign commented 5 years ago

Related to https://github.com/zendframework/zend-i18n-resources/issues/43