New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSR7 in upload form ->setData() #227

Open
samsonasik opened this Issue Dec 20, 2018 · 3 comments

Comments

Projects
None yet
2 participants
@samsonasik
Copy link
Contributor

samsonasik commented Dec 20, 2018

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(),
);
@froschdesign

This comment has been minimized.

Copy link
Member

froschdesign commented Dec 21, 2018

@samsonasik
First: the problem is not zend-form.

The current validators like Size or MimeType can not handle the PSR7 uploads.

And then there are two options for zend-filter missing:

  1. stream_factory:
    https://github.com/zendframework/zend-filter/blob/135670d33804113070c4f243e961250b248e9e04/src/File/RenameUpload.php#L412-L420
  2. upload_file_factory:
    https://github.com/zendframework/zend-filter/blob/135670d33804113070c4f243e961250b248e9e04/src/File/RenameUpload.php#L424-L432

Compare with documentation of zend-filter: "Handle a PSR-7 uploaded file"

@samsonasik

This comment has been minimized.

Copy link
Contributor

samsonasik commented Dec 21, 2018

@froschdesign ok, thank you.

@froschdesign

This comment has been minimized.

Copy link
Member

froschdesign commented Dec 21, 2018

@samsonasik
For the moment, you still need to use the workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment