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

Commit

Permalink
Merge branch 'master' into ValidatorMessages
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 12 changed files with 254 additions and 38 deletions.
53 changes: 31 additions & 22 deletions src/BaseInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ public function add($input, $name = null)
}

if (isset($this->inputs[$name]) && $this->inputs[$name] instanceof InputInterface) {
// The element already exists, so merge the config. Please note that the order is important (already existing
// input is merged with the parameter given)
$input->merge($this->inputs[$name]);
// The element already exists, so merge the config. Please note
// that this merges the new input into the original.
$original = $this->inputs[$name];
$original->merge($input);
return $this;
}

$this->inputs[$name] = $input;
Expand Down Expand Up @@ -156,32 +158,39 @@ 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);
}

/**
* Validate a set of inputs against the current data
*
* @param array $inputs
* @param array $inputs
* @param array $data
* @return bool
*/
protected function validateInputs(array $inputs)
protected function validateInputs(array $inputs, array $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 @@ -208,7 +217,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 @@ -220,7 +229,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 @@ -233,7 +242,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 @@ -245,7 +254,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 @@ -259,15 +268,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 @@ -278,7 +287,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 @@ -294,7 +303,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
13 changes: 12 additions & 1 deletion src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Zend\Stdlib\ArrayUtils;
use Zend\Validator\ValidatorInterface;
use Zend\Validator\ValidatorChain;
use Zend\ServiceManager\ServiceLocatorInterface;

class Factory
{
Expand Down Expand Up @@ -116,7 +117,15 @@ public function clearDefaultValidatorChain()
public function setInputFilterManager(InputFilterPluginManager $inputFilterManager)
{
$this->inputFilterManager = $inputFilterManager;

$serviceLocator = $this->inputFilterManager->getServiceLocator();
if ($serviceLocator && $serviceLocator instanceof ServiceLocatorInterface) {
if ($serviceLocator->has('ValidatorManager')) {
$this->getDefaultValidatorChain()->setPluginManager($serviceLocator->get('ValidatorManager'));
}
if ($serviceLocator->has('FilterManager')) {
$this->getDefaultFilterChain()->setPluginManager($serviceLocator->get('FilterManager'));
}
}
return $this;
}

Expand Down Expand Up @@ -212,6 +221,7 @@ public function createInput($inputSpecification)
break;
case 'error_message':
$input->setErrorMessage($value);
break;
case 'fallback_value':
$input->setFallbackValue($value);
break;
Expand Down Expand Up @@ -283,6 +293,7 @@ public function createInputFilter($inputFilterSpecification)
$inputFilter = $this->getInputFilterManager()->get($type);

if ($inputFilter instanceof CollectionInputFilter) {
$inputFilter->setFactory($this);
if (isset($inputFilterSpecification['input_filter'])) {
$inputFilter->setInputFilter($inputFilterSpecification['input_filter']);
}
Expand Down
1 change: 0 additions & 1 deletion test/ArrayInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_InputFilter
*/

namespace ZendTest\InputFilter;
Expand Down
99 changes: 97 additions & 2 deletions test/BaseInputFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_InputFilter
*/

namespace ZendTest\InputFilter;
Expand Down Expand Up @@ -188,13 +187,21 @@ public function dataSets()
*/
public function testCanValidateEntireDataset($dataset, $expected)
{
if (!extension_loaded('intl')) {
$this->markTestSkipped('ext/intl not enabled');
}

$filter = $this->getInputFilter();
$filter->setData($dataset);
$this->assertSame($expected, $filter->isValid());
}

public function testCanValidatePartialDataset()
{
if (!extension_loaded('intl')) {
$this->markTestSkipped('ext/intl not enabled');
}

$filter = $this->getInputFilter();
$validData = array(
'foo' => ' bazbat ',
Expand All @@ -218,6 +225,10 @@ public function testCanValidatePartialDataset()

public function testCanRetrieveInvalidInputsOnFailedValidation()
{
if (!extension_loaded('intl')) {
$this->markTestSkipped('ext/intl not enabled');
}

$filter = $this->getInputFilter();
$invalidData = array(
'foo' => ' bazbat ',
Expand All @@ -243,6 +254,10 @@ public function testCanRetrieveInvalidInputsOnFailedValidation()

public function testCanRetrieveValidInputsOnFailedValidation()
{
if (!extension_loaded('intl')) {
$this->markTestSkipped('ext/intl not enabled');
}

$filter = $this->getInputFilter();
$invalidData = array(
'foo' => ' bazbat ',
Expand All @@ -269,6 +284,10 @@ public function testCanRetrieveValidInputsOnFailedValidation()

public function testValuesRetrievedAreFiltered()
{
if (!extension_loaded('intl')) {
$this->markTestSkipped('ext/intl not enabled');
}

$filter = $this->getInputFilter();
$validData = array(
'foo' => ' bazbat ',
Expand Down Expand Up @@ -297,6 +316,10 @@ public function testValuesRetrievedAreFiltered()

public function testCanGetRawInputValues()
{
if (!extension_loaded('intl')) {
$this->markTestSkipped('ext/intl not enabled');
}

$filter = $this->getInputFilter();
$validData = array(
'foo' => ' bazbat ',
Expand All @@ -316,6 +339,10 @@ public function testCanGetRawInputValues()

public function testCanGetValidationMessages()
{
if (!extension_loaded('intl')) {
$this->markTestSkipped('ext/intl not enabled');
}

$filter = $this->getInputFilter();
$filter->get('baz')->setRequired(true);
$filter->get('nest')->get('baz')->setRequired(true);
Expand Down Expand Up @@ -609,7 +636,7 @@ public function testValidationMarksInputValidWhenAllowEmptyFlagIsTrueAndContinue
->setContinueIfEmpty(true);

$blankIsValid = new Input();
$blankIsValid->getValidatorChain()->attach(new Validator\Callback(function($value, $context) {
$blankIsValid->getValidatorChain()->attach(new Validator\Callback(function ($value, $context) {
return ('y' === $value && empty($context['allowEmpty']));
}));

Expand All @@ -622,6 +649,10 @@ public function testValidationMarksInputValidWhenAllowEmptyFlagIsTrueAndContinue

public function testCanRetrieveRawValuesIndividuallyWithoutValidating()
{
if (!extension_loaded('intl')) {
$this->markTestSkipped('ext/intl not enabled');
}

$filter = $this->getInputFilter();
$data = array(
'foo' => ' bazbat ',
Expand All @@ -638,6 +669,10 @@ public function testCanRetrieveRawValuesIndividuallyWithoutValidating()

public function testCanRetrieveUnvalidatedButFilteredInputValue()
{
if (!extension_loaded('intl')) {
$this->markTestSkipped('ext/intl not enabled');
}

$filter = $this->getInputFilter();
$data = array(
'foo' => ' baz 2 bat ',
Expand Down Expand Up @@ -670,8 +705,13 @@ public function testGetRequiredNotEmptyValidationMessages()
$this->assertArrayHasKey('foo', $messages);
$this->assertNotEmpty($messages['foo']);
}

public function testHasUnknown()
{
if (!extension_loaded('intl')) {
$this->markTestSkipped('ext/intl not enabled');
}

$filter = $this->getInputFilter();
$validData = array(
'foo' => ' bazbat ',
Expand All @@ -692,6 +732,10 @@ public function testHasUnknown()
}
public function testGetUknown()
{
if (!extension_loaded('intl')) {
$this->markTestSkipped('ext/intl not enabled');
}

$filter = $this->getInputFilter();
$unknown = array(
'bar' => '12345',
Expand Down Expand Up @@ -765,4 +809,55 @@ public function testGetInputs()
$this->assertEquals('foo', $filters['foo']->getName());
$this->assertEquals('bar', $filters['bar']->getName());
}

/**
* @group 4996
*/
public function testAddingExistingInputWillMergeIntoExisting()
{
$filter = new InputFilter();

$foo1 = new Input('foo');
$foo1->setRequired(true);
$filter->add($foo1);

$foo2 = new Input('foo');
$foo2->setRequired(false);
$filter->add($foo2);

$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');
}
}
Loading

0 comments on commit 15def87

Please sign in to comment.