0

I have a form that allows a user to edit his information and upload an image. The image upload is optional but when I submit the form, I always have an error telling me that the image has not been uploaded.

Here is the partial fieldset code:

public function init() {
    $this->add([
        'name' => 'avatar',
        'type' => 'file',
        'attributes' => [
            'id' => 'avatar',
            'class' => 'input-file',
            'accept' => 'image/*'
        ],
        'options' => [
            'label' => 'avatar',
        ]
    ]);
}

This fieldset implements InputFilterProviderInterface, so here is the code:

public function getInputFilterSpecification() {
    return [
        'avatar' => [
            'type' => '\Zend\InputFilter\FileInput',
            'allow_empty' => true,
            'required' => false,
            'validators' => [
                [
                    'name' => 'FileExtension',
                    'options' => [
                        'extension' => ['jpg, jpeg, png'],
                        'message' => 'wrong_type_file'
                    ]
                ],
                [
                    'name' => 'FileSize',
                    'options' => [
                        'max' => '2MB',
                        'message' => 'file_too_large'
                    ]
                ]
            ],
        ],
    ];
}

Despite the fact that this field is not required and allow_empty option is true, when I submit the form and debug the value, I still have validation that fails and this as my debug value:

'avatar' => 
    array (size=5)
      'name' => string '' (length=0)
      'type' => string '' (length=0)
      'tmp_name' => string '' (length=0)
      'error' => int 4
      'size' => int 0

Do you know how could I validate my form event when no file is downloaded?

Thanks in advance for your answers!

EDIT 1: Here are the messages returned by the form after submission:

'avatar' => 
     array (size=2)
         'fileExtensionNotFound' => string 'Extension d'image non admise (.jpg, .jpeg, .png seulement)'
         'fileSizeNotFound' => string 'Le fichier est trop volumineux, 2097152 maximum.' (length=48)

EDIT 2: the action in the controller:

$prg = $this->fileprg($form);
    if ($prg instanceof Response) {
        return $prg;
    } elseif (is_array($prg)) {
        $data = $form->getData();
        if ($form->isValid()) {
            if ($data['person']['file']) {
                // @TODO upload file
            }
            if (!$this->personMapper->updatePerson($data)) {
                $this->flashMessenger()->addErrorMessage($this->translator()->translate('error_occurs_backup'));
            } else {
                $this->flashMessenger()->addSuccessMessage($this->translator()->translate('data_saved'));
            }
        } else {
            // @TODO file error management
            \var_dump('not valid');
            \var_dump($form->getMessages());
        }
    }
RomainG
  • 1
  • 2
  • What is the error message set on the form? Get this after the `isValid()` using `$form->getMessage()`. Also, what else you tried and what did you find out? Did you come across [this](https://samsonasik.wordpress.com/2012/08/31/zend-framework-2-creating-upload-form-file-validation/)? – rkeet Oct 30 '18 at 13:54
  • @rkeet I edit my answer with the messages returned by form after the `isValid()` method call – RomainG Oct 30 '18 at 14:37
  • In the validator for the file (`avatar`), did you happen to set the allowed extensions and file size? If yes, what happens if you run using defaults (so turn off the options as a test)? And if no, is the file you're testing with within the set boundaries? – rkeet Oct 30 '18 at 18:45
  • I have already tested by disabling the options of the validator. The problem occurs when I want to submit the form without a file. I end up with the error messages that I indicated in the first message – RomainG Oct 31 '18 at 08:01

0 Answers0