diff --git a/library/Zend/InputFilter/BaseInputFilter.php b/library/Zend/InputFilter/BaseInputFilter.php index eb708175cd4..12c5a917993 100644 --- a/library/Zend/InputFilter/BaseInputFilter.php +++ b/library/Zend/InputFilter/BaseInputFilter.php @@ -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) { @@ -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)) { @@ -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; @@ -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; @@ -531,6 +535,7 @@ public function getRawValues() $values[$name] = $input->getRawValues(); continue; } + $values[$name] = $input->getRawValue(); } return $values; diff --git a/tests/ZendTest/InputFilter/InputFilterTest.php b/tests/ZendTest/InputFilter/InputFilterTest.php index af999142988..c9c9d32a030 100644 --- a/tests/ZendTest/InputFilter/InputFilterTest.php +++ b/tests/ZendTest/InputFilter/InputFilterTest.php @@ -18,6 +18,11 @@ class InputFilterTest extends TestCase { + /** + * @var InputFilter + */ + protected $filter; + public function setUp() { $this->filter = new InputFilter(); @@ -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); + } }