diff --git a/CHANGELOG.md b/CHANGELOG.md index f238713a..b8c63eb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,9 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed +- [#185](https://github.com/zendframework/zend-inputfilter/pull/185) fixes + validation response on invalid file upload request. + - [#181](https://github.com/zendframework/zend-inputfilter/pull/181) fixes missing abstract service factory registration in `Module` as per the [latest documentation](https://docs.zendframework.com/zend-inputfilter/specs/#setup). diff --git a/src/FileInput/HttpServerFileInputDecorator.php b/src/FileInput/HttpServerFileInputDecorator.php index 0e6323b7..b0034877 100644 --- a/src/FileInput/HttpServerFileInputDecorator.php +++ b/src/FileInput/HttpServerFileInputDecorator.php @@ -110,6 +110,15 @@ public function isValid($context = null) 'type' => '', 'error' => UPLOAD_ERR_NO_FILE, ]; + } elseif (! isset($rawValue['tmp_name']) && ! isset($rawValue[0]['tmp_name'])) { + // This can happen when sent not file and just array + $rawValue = [ + 'tmp_name' => '', + 'name' => '', + 'size' => 0, + 'type' => '', + 'error' => UPLOAD_ERR_NO_FILE, + ]; } if (is_array($rawValue) && isset($rawValue['tmp_name'])) { diff --git a/test/FileInput/HttpServerFileInputDecoratorTest.php b/test/FileInput/HttpServerFileInputDecoratorTest.php index bf19cd94..20385d90 100644 --- a/test/FileInput/HttpServerFileInputDecoratorTest.php +++ b/test/FileInput/HttpServerFileInputDecoratorTest.php @@ -196,6 +196,23 @@ public function testValidationsRunWithoutFileArrayDueToAjaxPost() $this->assertFalse($this->input->isValid()); } + public function testValidationsRunWithoutFileArrayIsSend() + { + $this->input->setAutoPrependUploadValidator(true); + $this->assertTrue($this->input->getAutoPrependUploadValidator()); + $this->assertTrue($this->input->isRequired()); + $this->input->setValue([]); + $expectedNormalizedValue = [ + 'tmp_name' => '', + 'name' => '', + 'size' => 0, + 'type' => '', + 'error' => UPLOAD_ERR_NO_FILE, + ]; + $this->input->setValidatorChain($this->createValidatorChainMock([[$expectedNormalizedValue, null, false]])); + $this->assertFalse($this->input->isValid()); + } + public function testNotEmptyValidatorAddedWhenIsValidIsCalled($value = null) { $this->markTestSkipped('Test is not enabled in FileInputTest');