Hi,
I try to use Zend Expressive, but I don’t understand how use FileRenameUpload and I can’t valid my form.
if ($request->getMethod() === 'POST') {
$this->imageForm->setData(array_merge($request->getParsedBody(), $request->getUploadedFiles()));
if ($this->imageForm->isValid()) {
dd($this->imageForm->getData());
}
}
I put this in my form class to move file after upload :
public function getInputFilterSpecification()
{
return [
[
'name' => 'file',
'required' => true,
'filters' => [
[
'name' => 'FileRenameUpload',
'options' => [
'target' => __DIR__.'../../../data/upload',
'useUploadName' => true,
'useUploadExtension' => true,
'overwrite' => true,
'randomize' => true
]
]
],
]
];
}
But when I do isValid on form, just this error :
"fileUploadFileErrorNoFile" => "File was not uploaded"
I don’t understand how use a Stream and valid it
I do right ? I readed doc but nothing about upload and valid form for Expressive.
Thanks
The various file validators and input types do not currently work with PSR-7, which is what Expressive uses. You will have to handle those separately currently.
We’re working on a new validation/input filter component (spec), and will likely tackle file upload validation, filtering, etc. in that component.
1 Like
For now, you can use Psr7Bridge
like this:
use Zend\Psr7Bridge\Psr7ServerRequest;
$zendRequest = Psr7ServerRequest::toZend($request);
$this->imageForm->setData(\array_merge_recursive(
$zendRequest->getPost()->toArray(),
$zendRequest->getFiles()->toArray()
));
Funny enough, I was just going to write a similar question. Saved some time now though I did not find a ready-to-use solution.
Hi,
thanks you for your fast replies. (and for your blog samsonasik
)
I didn’t read anything about non-fonctinal validators and inputfilters.
I took Psr7Bridge and it works like a charm.
There is a cleaner how to way to do this ?
You’re welcome Ralf.
As of zend-filter 2.9, it can now be done without a bridge.
https://docs.zendframework.com/zend-filter/file/
Look for PSR-7 section.
Configuration can now be:
use Zend\Diactoros\StreamFactory;
use Zend\Diactoros\UploadedFileFactory;
public function getInputFilterSpecification()
{
return [
[
'name' => 'file',
'required' => true,
'filters' => [
[
'name' => 'FileRenameUpload',
'options' => [
'target' => __DIR__.'../../../data/upload',
'useUploadName' => true,
'useUploadExtension' => true,
'overwrite' => true,
'randomize' => true,
'stream_factory' => new StreamFactory(),
'upload_file_factory' => new UploadedFileFactory(),
]
]
],
]
];
}
that may only works for filter, for validation, eg: Size, MimeType, it still needs Psr7bridge, refs https://github.com/zendframework/zend-form/issues/227
Ah, did not realize there were still more. Sorry for oversight, and thank you for testing. Will try to get the rest of them done.