0

I have a form that accepts a text field and an image, and I would like to be able to validate or filter the images that are uploaded.

I try to follow this tutorial on GitHub (https://github.com/lowtower/zend-expressive2-tutorial)

I am using zend expressive 2. I have this approach:

<?php

namespace Admin\Model\InputFilter;

use Zend\InputFilter\InputFilter;

class EventoInputFilter extends InputFilter
{
    public function init()
    {
        $this->add([
            'name'     => 'eventoNombre',
            'required' => true,
            'filters'  => [
                ['name' => 'StripTags'],
                ['name' => 'StringTrim'],
            ],
            'validators' => [
                [
                    'name'    => 'StringLength',
                    'options' => [
                        'min' => 5,
                        'max' => 100,
                    ],
                ],
            ],
        ]);

        $this->add([
            'name'     => 'imagenes[]',
            'required' => true,
            'validators' => [
                [
                    'name'    => 'FileUploadFile'
                ],
                [
                    'name'    => 'FileMimeType',
                    'options' => [
                        'mimeType'  => ['image/jpeg', 'image/png']
                    ]
                ],
                [
                    'name'    => 'FileIsImage'
                ],
                [
                    'name'    => 'FileImageSize',
                    'options' => [
                        'minWidth'  => 128,
                        'minHeight' => 128,
                        'maxWidth'  => 4096,
                        'maxHeight' => 4096
                    ]
                ],
            ],
            'filters'  => [
                [
                    'name' => 'FileRenameUpload',
                    'options' => [
                        'target'                => 'public/eventos/',
                        'useUploadName'         => true,
                        'useUploadExtension'    => true,
                        'overwrite'             => true,
                        'randomize'             => false
                    ]
                ]
            ],
        ]);
    }
}

but something is wrong and I do not validate it correctly when I upload a correct image.

edigu
  • 9,878
  • 5
  • 57
  • 80
  • I haven't use this for multiple files myself, but looks like you used the wrong field name. Try this: `'name' => 'imagenes'`. – xtreamwayz Feb 15 '18 at 09:12
  • the field to upload images is a multiple field, therefore it must have that bracket, because I will upload one or two images. I realized that only the required option works from that filter. – greermurray Feb 15 '18 at 14:17
  • You need to use explode validator for array fields. – Mehmet SÖĞÜNMEZ Feb 15 '18 at 16:51
  • How could I validate using that option that you tell me using this filter? – greermurray Feb 15 '18 at 17:27
  • Please try set the name as `imagenes`. Zend Input Filter will automatically detect your uploaded files from `imagenes[]` form element. – Dolly Aswin Feb 16 '18 at 16:17
  • If I remove the brackets, the field does not detect me (nothing happens) – greermurray Feb 16 '18 at 21:39
  • at the end I was able to solve "my problem" by putting $this->setAttribute('enctype', 'multipart/form-data'); in the form and follow this recommendation: https://github.com/zendframework/zend-inputfilter/issues/145#issuecomment-318049525 – greermurray Feb 21 '18 at 03:43

0 Answers0