Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jan 8, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
// "Not required" should not apply to empty strings (#3983)
&& !(array_key_exists($name, $this->data) && is_string($this->data[$name]))
) {
$this->validInputs[$name] = $input;
continue;
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I preserved what seemed to be the intent of #3983, but @ossinkine please look this over to make sure it's correct.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the truth.
But the expression strlen($this->data[$name]) === 0 is unnecessary, because the expression is checked before.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good catch - I will remove that expression.

// - 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);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted these tests to match earlier behavior.


$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