Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

PSR7 in upload form ->setData() #227

@samsonasik

Description

@samsonasik

I'm trying latest zend-validator 2.11.0 and latest zend-inputfilter 2.9.0 which have psr7 support. I'm trying the following Form::setData() and Form::isvalid(). So, the following code:

            // $request is an instance of \Psr\Http\Message\ServerRequestInterface
            $postAndFileData = \array_merge_recursive(
                $request->getParsedBody(),
                $request->getUploadedFiles()
            );

           $form->setData($postAndFileData);
           $form->isValid();

I got the following errors:

basename() expects parameter 1 to be string, object given

I have the following form specs:

use Zend\Filter\File\RenameUpload;
use Zend\Validator\File\MimeType;
use Zend\Validator\File\Size;

    public function getInputFilterSpecification()
    {
        return [
            [
                'name' => 'filename',
                'required' => true,
                'filters' => [
                    [
                        'name' => RenameUpload::class,
                        'options' => [
                            'target'               => \getcwd() . '/public/uploads',
                            'use_upload_extension' => true,
                            'use_upload_name'      => true,
                            'overwrite'            => true,
                            'randomize'            => false,
                        ],
                    ],
                ],
                'validators' => [
                    [
                        'name' => Size::class,
                        'options' => [
                            'max' => '10MB',
                        ],
                    ],
                    [
                        'name' => MimeType::class,
                        'options' => [
                            'mimeType' => [
                                'image/jpg',
                                'image/jpeg',
                                'image/png',
                            ],
                        ]
                     ]
                ],
            ],
        ];
    }

When I try remove the validators, the filters got errors:

No PSR-17 Psr\Http\Message\StreamFactoryInterface present; cannot filter file. Please pass the stream_file_factory option with a Psr\Http\Message\StreamFactoryInterface instance when creating the filter for use with PSR-7.

The current workaround is by using the zend-psr7bridge :

$zendRequest        = Psr7ServerRequest::toZend($request);
$postAndFileData = \array_merge_recursive(
                $zendRequest->getPost()->toArray(),
                $zendRequest->getFiles()->toArray(),
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions