zendframework / zend-inputfilter

InputFilter component from Zend Framework
BSD 3-Clause "New" or "Revised" License
64 stars 50 forks source link

FileInput not compatible with PSR (and Dictatoros) UploadedFile #145

Closed bfolliot closed 5 years ago

bfolliot commented 7 years ago

Hello.

Zend\InputFilter\FileInput does not accept an implementation of Psr\Http\Message\UploadedFileInterface if the input is required, for exemple, i have this form with InputFilterProviderInterface :

class MyForm extends Form implements InputFilterProviderInterface
{

    public function __construct()
    {
        parent::__construct();

        $this->add([
            'name' => 'myPicture',
            'type'  => 'File',
        ]);

        $this->add([
            'name' => 'submit',
            'type'  => 'Submit',
            'attributes' => [
                'value' => 'Submit',
                'class' => 'btn-primary',
            ],
        ]);
    }

    /**
      * @return array
      */
    public function getInputFilterSpecification() : array
    {
        return [
            'myPicture' => [
                'required' => true, // <========
            ],
        ];
    }
}

And in an Expressive middleware :

$form = new MyForm;

if ($request->getMethod() === 'POST') {
    $form->setData(array_merge_recursive(
        // @var Psr\Http\Message\ServerRequestInterface $request
        $request->getParsedBody(),
        $request->getUploadedFiles()
    ));
    var_dump($form->isValid()); // Display always false...
}
svycka commented 7 years ago

I workaround-ed this with zendframework/zend-psr7bridge like this:

$zendRequest = \Zend\Psr7Bridge\Psr7ServerRequest::toZend($request);
$inputFilter->setData($zendRequest->getFiles());
...
firefred commented 6 years ago

We have similar problem here, this problem forces us to use $_FILES array instead of $request->getUploadedFiles()

froschdesign commented 5 years ago

Fixed and released with version 2.9.0. A code example can be found in the documentation: https://docs.zendframework.com/zend-inputfilter/file-input/#psr-7-support