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

Commit

Permalink
Show file tree
Hide file tree
Showing 5 changed files with 372 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/BaseInputFilter.php
Expand Up @@ -165,6 +165,11 @@ public function isValid()
if (!array_key_exists($name, $this->data)
|| (null === $this->data[$name])
|| (is_string($this->data[$name]) && strlen($this->data[$name]) === 0)
// Single and Multi File Uploads
|| (is_array($this->data[$name])
&& isset($this->data[$name]['error']) && $this->data[$name]['error'] === UPLOAD_ERR_NO_FILE)
|| (is_array($this->data[$name]) && count($this->data[$name]) === 1
&& isset($this->data[$name][0]['error']) && $this->data[$name][0]['error'] === UPLOAD_ERR_NO_FILE)
) {
if ($input instanceof InputInterface) {
// - test if input is required
Expand Down
62 changes: 62 additions & 0 deletions src/FileInput.php
@@ -0,0 +1,62 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_InputFilter
*/

namespace Zend\InputFilter;

use Zend\Filter\FilterChain;
use Zend\Validator\ValidatorChain;
use Zend\Validator\NotEmpty;

/**
* @category Zend
* @package Zend_InputFilter
*/
class FileInput extends Input
{
/**
* @var boolean
*/
protected $isValid = false;

/**
* @return mixed
*/
public function getValue()
{
$filter = $this->getFilterChain();
$value = (is_array($this->value) && isset($this->value['tmp_name']))
? $this->value['tmp_name'] : $this->value;
if ($this->isValid) {
$value = $filter->filter($value);
}
return $value;
}

/**
* @param mixed $context Extra "context" to provide the validator
* @return boolean
*/
public function isValid($context = null)
{
$this->injectNotEmptyValidator();
$validator = $this->getValidatorChain();
//$value = $this->getValue(); // Do not run the filters yet for File uploads
$this->isValid = $validator->isValid($this->getRawValue(), $context);
return $this->isValid;
}

/**
* @return void
*/
protected function injectNotEmptyValidator()
{
$this->notEmptyValidator = true;
}
}
3 changes: 3 additions & 0 deletions src/Input.php
Expand Up @@ -309,6 +309,9 @@ public function getMessages()
return $validator->getMessages();
}

/**
* @return void
*/
protected function injectNotEmptyValidator()
{
if ((!$this->isRequired() && $this->allowEmpty()) || $this->notEmptyValidator) {
Expand Down
60 changes: 60 additions & 0 deletions test/BaseInputFilterTest.php
Expand Up @@ -13,6 +13,7 @@
use PHPUnit_Framework_TestCase as TestCase;
use stdClass;
use Zend\InputFilter\Input;
use Zend\InputFilter\FileInput;
use Zend\InputFilter\BaseInputFilter as InputFilter;
use Zend\Filter;
use Zend\Validator;
Expand Down Expand Up @@ -414,6 +415,65 @@ public function testValidationSkipsFieldsMarkedNotRequiredWhenNoDataPresent()
$this->assertTrue($filter->isValid());
}

public function testValidationSkipsFileInputsMarkedNotRequiredWhenNoFileDataIsPresent()
{
$filter = new InputFilter();

$foo = new FileInput();
$foo->getValidatorChain()->addValidator(new Validator\File\Upload());
$foo->setRequired(false);

$filter->add($foo, 'foo');

$data = array(
'foo' => array(
'tmp_name' => '/tmp/barfile',
'name' => 'barfile',
'type' => 'text',
'size' => 0,
'error' => 4, // UPLOAD_ERR_NO_FILE
)
);
$filter->setData($data);
$this->assertTrue($filter->isValid());

// Negative test
$foo->setRequired(true);
$filter->setData($data);
$this->assertFalse($filter->isValid());
}

public function testValidationSkipsFileInputsMarkedNotRequiredWhenNoMultiFileDataIsPresent()
{
$filter = new InputFilter();

$explode = new Validator\File\Explode();
$explode->setValidator(new Validator\File\Upload());

$foo = new FileInput();
$foo->getValidatorChain()->addValidator($explode);
$foo->setRequired(false);

$filter->add($foo, 'foo');

$data = array(
'foo' => array(array(
'tmp_name' => '/tmp/barfile',
'name' => 'barfile',
'type' => 'text',
'size' => 0,
'error' => 4, // UPLOAD_ERR_NO_FILE
)),
);
$filter->setData($data);
$this->assertTrue($filter->isValid());

// Negative test
$foo->setRequired(true);
$filter->setData($data);
$this->assertFalse($filter->isValid());
}

public function testValidationAllowsEmptyValuesToRequiredInputWhenAllowEmptyFlagIsTrue()
{
$filter = new InputFilter();
Expand Down

0 comments on commit ba32611

Please sign in to comment.