Skip to content

Commit

Permalink
Properly pass context to input filter
Browse files Browse the repository at this point in the history
  • Loading branch information
bakura10 authored and wdalmut committed Apr 24, 2014
1 parent 4abb35b commit 7aec8ca
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
19 changes: 12 additions & 7 deletions library/Zend/InputFilter/BaseInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,11 @@ public function setData($data)
/**
* Is the data set valid?
*
* @param mixed|null $context
* @throws Exception\RuntimeException
* @return bool
*/
public function isValid()
public function isValid($context = null)
{
$data = $this->getRawValues();
if (null === $data) {
Expand All @@ -203,17 +204,18 @@ public function isValid()
}

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

/**
* Validate a set of inputs against the current data
*
* @param array $inputs
* @param array $data
* @param array $inputs
* @param array $data
* @param mixed|null $context
* @return bool
*/
protected function validateInputs(array $inputs, array $data = array())
protected function validateInputs(array $inputs, array $data = array(), $context = null)
{
// backwards compatibility
if (empty($data)) {
Expand Down Expand Up @@ -328,7 +330,7 @@ protected function validateInputs(array $inputs, array $data = array())

// Validate an input filter
if ($input instanceof InputFilterInterface) {
if (!$input->isValid()) {
if (!$input->isValid($context)) {
$this->invalidInputs[$name] = $input;
$valid = false;
continue;
Expand All @@ -339,7 +341,9 @@ protected function validateInputs(array $inputs, array $data = array())

// Validate an input
if ($input instanceof InputInterface) {
if (!$input->isValid($data)) {
$inputContext = $context ?: $data;

if (!$input->isValid($inputContext)) {
// Validation failure
$this->invalidInputs[$name] = $input;
$valid = false;
Expand Down Expand Up @@ -531,6 +535,7 @@ public function getRawValues()
$values[$name] = $input->getRawValues();
continue;
}

$values[$name] = $input->getRawValue();
}
return $values;
Expand Down
19 changes: 19 additions & 0 deletions tests/ZendTest/InputFilter/InputFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

class InputFilterTest extends TestCase
{
/**
* @var InputFilter
*/
protected $filter;

public function setUp()
{
$this->filter = new InputFilter();
Expand Down Expand Up @@ -96,4 +101,18 @@ public function testCountZeroValidateInternalInputWithCollectionInputFilter()
$this->assertTrue($this->filter->isvalid());
$this->assertSame($data, $this->filter->getValues());
}

public function testCanUseContextPassedToInputFilter()
{
$context = new \stdClass();

$input = $this->getMock('Zend\InputFilter\InputInterface');
$input->expects($this->once())->method('isValid')->with($context)->will($this->returnValue(true));
$input->expects($this->any())->method('getRawValue')->will($this->returnValue('Mwop'));

$this->filter->add($input, 'username');
$this->filter->setData(array('username' => 'Mwop'));

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

0 comments on commit 7aec8ca

Please sign in to comment.