Skip to content

Commit

Permalink
Revert to previous isRequired behavior for file uploads (fix BC break)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgmartin committed Apr 3, 2013
1 parent ac9c3f9 commit 54ea3ba
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
16 changes: 8 additions & 8 deletions library/Zend/InputFilter/BaseInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,6 @@ public function isValid()
$inputs = $this->validationGroup ?: array_keys($this->inputs);
foreach ($inputs as $name) {
$input = $this->inputs[$name];
if ((!array_key_exists($name, $this->data)
|| (null === $this->data[$name]))
&& $input instanceof InputInterface
&& !$input->isRequired()
) {
$this->validInputs[$name] = $input;
continue;
}
if (!array_key_exists($name, $this->data)
|| (null === $this->data[$name])
|| (is_string($this->data[$name]) && strlen($this->data[$name]) === 0)
Expand All @@ -178,6 +170,14 @@ public function isValid()
&& isset($this->data[$name][0]['error']) && $this->data[$name][0]['error'] === UPLOAD_ERR_NO_FILE)
) {
if ($input instanceof InputInterface) {
// - test if input is required
if (!$input->isRequired()
// Do not mark valid for isRequired setting if value exists and is empty string
&& !(array_key_exists($name, $this->data) && is_string($this->data[$name]) && strlen($this->data[$name]) === 0)
) {
$this->validInputs[$name] = $input;
continue;
}
// - test if input allows empty
if ($input->allowEmpty()) {
$this->validInputs[$name] = $input;
Expand Down
12 changes: 6 additions & 6 deletions tests/ZendTest/InputFilter/BaseInputFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,13 +450,13 @@ public function testValidationSkipsFieldsMarkedNotRequiredWhenNoDataPresent()
$this->assertTrue($filter->isValid());
}

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

$foo = new FileInput();
$foo->getValidatorChain()->attach(new Validator\File\UploadFile());
$foo->setAllowEmpty(true);
$foo->setRequired(false);

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

Expand All @@ -473,16 +473,16 @@ public function testValidationSkipsFileInputsMarkedAllowEmptyWhenNoFileDataIsPre
$this->assertTrue($filter->isValid());

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

public function testValidationSkipsFileInputsMarkedAllowEmptyWhenNoMultiFileDataIsPresent()
public function testValidationSkipsFileInputsMarkedNotRequiredWhenNoMultiFileDataIsPresent()
{
$filter = new InputFilter();
$foo = new FileInput();
$foo->setAllowEmpty(true);
$foo->setRequired(false);
$filter->add($foo, 'foo');

$data = array(
Expand All @@ -498,7 +498,7 @@ public function testValidationSkipsFileInputsMarkedAllowEmptyWhenNoMultiFileData
$this->assertTrue($filter->isValid());

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

0 comments on commit 54ea3ba

Please sign in to comment.