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

Commit

Permalink
Merge pull request zendframework/zendframework#5271 from steverhoades…
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 18 deletions.
42 changes: 24 additions & 18 deletions src/BaseInputFilter.php
Expand Up @@ -158,15 +158,16 @@ public function setData($data)
*/
public function isValid()
{
if (null === $this->data) {
$data = $this->getRawValues();
if (null === $data) {
throw new Exception\RuntimeException(sprintf(
'%s: no data present to validate!',
__METHOD__
));
}

$inputs = $this->validationGroup ?: array_keys($this->inputs);
return $this->validateInputs($inputs);
return $this->validateInputs($inputs, $data);
}

/**
Expand All @@ -175,15 +176,20 @@ public function isValid()
* @param array $inputs
* @return bool
*/
protected function validateInputs(array $inputs)
protected function validateInputs(array $inputs, $data = array())
{
//backwards compatibility
if(empty($data)) {
$data = $this->getRawValues();
}

$this->validInputs = array();
$this->invalidInputs = array();
$valid = true;

foreach ($inputs as $name) {
$input = $this->inputs[$name];
$dataExists = array_key_exists($name, $this->data);
$dataExists = array_key_exists($name, $data);

// key doesn't exist, but input is not required; valid
if (!$dataExists
Expand All @@ -210,7 +216,7 @@ protected function validateInputs(array $inputs)

// key exists, is null, input is not required; valid
if ($dataExists
&& null === $this->data[$name]
&& null === $data[$name]
&& $input instanceof InputInterface
&& !$input->isRequired()
) {
Expand All @@ -222,7 +228,7 @@ protected function validateInputs(array $inputs)
// continueIfEmpty is false or input doesn't implement
// that interface; otherwise validation chain continues
if ($dataExists
&& null === $this->data[$name]
&& null === $data[$name]
&& $input instanceof InputInterface
&& $input->isRequired()
&& $input->allowEmpty()
Expand All @@ -235,7 +241,7 @@ protected function validateInputs(array $inputs)

// key exists, empty string, input is not required, allows empty; valid
if ($dataExists
&& '' === $this->data[$name]
&& '' === $data[$name]
&& $input instanceof InputInterface
&& !$input->isRequired()
&& $input->allowEmpty()
Expand All @@ -247,7 +253,7 @@ protected function validateInputs(array $inputs)
// key exists, empty string, input is required, allows empty; valid
// if continueIfEmpty is false, otherwise validation continues
if ($dataExists
&& '' === $this->data[$name]
&& '' === $data[$name]
&& $input instanceof InputInterface
&& $input->isRequired()
&& $input->allowEmpty()
Expand All @@ -261,15 +267,15 @@ protected function validateInputs(array $inputs)
// key exists, is array representing file, no file present, input not
// required or allows empty; valid
if ($dataExists
&& is_array($this->data[$name])
&& is_array($data[$name])
&& (
(isset($this->data[$name]['error'])
&& $this->data[$name]['error'] === UPLOAD_ERR_NO_FILE)
|| (count($this->data[$name]) === 1
&& isset($this->data[$name][0])
&& is_array($this->data[$name][0])
&& isset($this->data[$name][0]['error'])
&& $this->data[$name][0]['error'] === UPLOAD_ERR_NO_FILE)
(isset($data[$name]['error'])
&& $data[$name]['error'] === UPLOAD_ERR_NO_FILE)
|| (count($data[$name]) === 1
&& isset($data[$name][0])
&& is_array($data[$name][0])
&& isset($data[$name][0]['error'])
&& $data[$name][0]['error'] === UPLOAD_ERR_NO_FILE)
)
&& $input instanceof InputInterface
&& (!$input->isRequired() || $input->allowEmpty())
Expand All @@ -280,7 +286,7 @@ protected function validateInputs(array $inputs)

// make sure we have a value (empty) for validation
if (!$dataExists) {
$this->data[$name] = null;
$data[$name] = null;
}

// Validate an input filter
Expand All @@ -296,7 +302,7 @@ protected function validateInputs(array $inputs)

// Validate an input
if ($input instanceof InputInterface) {
if (!$input->isValid($this->data)) {
if (!$input->isValid($data)) {
// Validation failure
$this->invalidInputs[$name] = $input;
$valid = false;
Expand Down
35 changes: 35 additions & 0 deletions test/BaseInputFilterTest.php
Expand Up @@ -705,6 +705,7 @@ public function testGetRequiredNotEmptyValidationMessages()
$this->assertArrayHasKey('foo', $messages);
$this->assertNotEmpty($messages['foo']);
}

public function testHasUnknown()
{
if (!extension_loaded('intl')) {
Expand Down Expand Up @@ -826,4 +827,38 @@ public function testAddingExistingInputWillMergeIntoExisting()

$this->assertFalse($filter->get('foo')->isRequired());
}


/**
* @group 5270
*/
public function testIsValidWhenValuesSetOnFilters()
{
$filter = new InputFilter();

$foo = new Input();
$foo->getFilterChain()->attachByName('stringtrim')
->attachByName('alpha');
$foo->getValidatorChain()->attach(new Validator\StringLength(15, 18));

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

//test valid with setData
$filter->setData(array('foo' => 'invalid'));
$this->assertFalse($filter->isValid());

//test invalid with setData
$filter->setData(array('foo' => 'thisisavalidstring'));
$this->assertTrue($filter->isValid());

//test invalid when setting data on actual filter
$filter->get('foo')->setValue('invalid');
$this->assertFalse($filter->get('foo')->isValid(), 'Filtered value is valid, should be invalid');
$this->assertFalse($filter->isValid(), 'Input filter did not return value from filter');

//test valid when setting data on actual filter
$filter->get('foo')->setValue('thisisavalidstring');
$this->assertTrue($filter->get('foo')->isValid(), 'Filtered value is not valid');
$this->assertTrue($filter->isValid(), 'Input filter did return value from filter');
}
}

0 comments on commit ba41c14

Please sign in to comment.