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

fixes #5270 #5271

Closed
wants to merge 5 commits into from
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
42 changes: 24 additions & 18 deletions library/Zend/InputFilter/BaseInputFilter.php
Original file line number Diff line number Diff line change
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 tests/ZendTest/InputFilter/BaseInputFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ public function testGetRequiredNotEmptyValidationMessages()
$this->assertArrayHasKey('foo', $messages);
$this->assertNotEmpty($messages['foo']);
}

public function testHasUnknown()
{
$filter = $this->getInputFilter();
Expand Down Expand Up @@ -782,4 +783,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');
}
}