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

Commit

Permalink
Merge branch 'weierophinney-hotfix/7448'
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 5 changed files with 480 additions and 47 deletions.
13 changes: 10 additions & 3 deletions src/FileInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,17 @@ public function isEmptyFile($rawValue)
*/
public function isValid($context = null)
{
$rawValue = $this->getRawValue();
$empty = $this->isEmptyFile($rawValue);
$rawValue = $this->getRawValue();
$empty = $this->isEmptyFile($rawValue);
$required = $this->isRequired();
$allowEmpty = $this->allowEmpty();
$continueIfEmpty = $this->continueIfEmpty();

if ($empty && $this->allowEmpty() && !$this->continueIfEmpty()) {
if ($empty && ! $required && ! $continueIfEmpty) {
return true;
}

if ($empty && $required && $allowEmpty && ! $continueIfEmpty) {
return true;
}

Expand Down
27 changes: 17 additions & 10 deletions src/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ public function setName($name)
public function setRequired($required)
{
$this->required = (bool) $required;
$this->setAllowEmpty(!$required);
return $this;
}

Expand Down Expand Up @@ -319,23 +318,31 @@ public function merge(InputInterface $input)
*/
public function isValid($context = null)
{
$value = $this->getValue();
$empty = ($value === null || $value === '' || $value === array());
$value = $this->getValue();
$empty = ($value === null || $value === '' || $value === array());
$required = $this->isRequired();
$allowEmpty = $this->allowEmpty();
$continueIfEmpty = $this->continueIfEmpty();

if ($empty && $this->allowEmpty() && !$this->continueIfEmpty()) {
if ($empty && ! $required && ! $continueIfEmpty) {
return true;
}

// Empty value needs further validation if continueIfEmpty is set
// so don't inject NotEmpty validator which would always
// mark that as false
if (!$this->continueIfEmpty() && !$this->allowEmpty()) {
if ($empty && $required && $allowEmpty && ! $continueIfEmpty) {
return true;
}

// At this point, we need to run validators.
// If we do not allow empty and the "continue if empty" flag are
// BOTH false, we inject the "not empty" validator into the chain,
// which adds that logic into the validation routine.
if (! $allowEmpty && ! $continueIfEmpty) {
$this->injectNotEmptyValidator();
}
$validator = $this->getValidatorChain();

$validator = $this->getValidatorChain();
$result = $validator->isValid($value, $context);
if (!$result && $this->hasFallback()) {
if (! $result && $this->hasFallback()) {
$this->setValue($this->getFallbackValue());
$result = true;
}
Expand Down
16 changes: 0 additions & 16 deletions test/BaseInputFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -626,22 +626,6 @@ public function testValidationMarksInputInvalidWhenRequiredAndAllowEmptyFlagIsFa
$this->assertFalse($filter->isValid());
}

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

$foo = new Input();
$foo->setRequired(false);
$foo->setAllowEmpty(false);

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

$data = array('foo' => '');
$filter->setData($data);

$this->assertFalse($filter->isValid());
}

public static function contextDataProvider()
{
return array(
Expand Down
12 changes: 0 additions & 12 deletions test/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,18 +193,6 @@ public function testFactoryWillCreateInputWithSuggestedValidators()
}
}

public function testFactoryWillCreateInputWithSuggestedRequiredFlagAndImpliesAllowEmptyFlag()
{
$factory = new Factory();
$input = $factory->createInput(array(
'name' => 'foo',
'required' => false,
));
$this->assertInstanceOf('Zend\InputFilter\InputInterface', $input);
$this->assertFalse($input->isRequired());
$this->assertTrue($input->allowEmpty());
}

public function testFactoryWillCreateInputWithSuggestedRequiredFlagAndAlternativeAllowEmptyFlag()
{
$factory = new Factory();
Expand Down
Loading

0 comments on commit 02315c7

Please sign in to comment.