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

Commit

Permalink
Merge branch 'hotfix/5968' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Apr 14, 2014
2 parents e5d2180 + 5050f73 commit 0cd91e3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 91 deletions.
102 changes: 26 additions & 76 deletions src/CollectionInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,6 @@

class CollectionInputFilter extends InputFilter
{
/*
* @var array
*/
protected $collectionData;

/*
* @var array
*/
protected $collectionValidInputs;

/*
* @var array
*/
protected $collectionInvalidInputs;

/*
* @var bool
*/
Expand Down Expand Up @@ -80,7 +65,7 @@ public function setInputFilter($inputFilter)
}

$this->inputFilter = $inputFilter;
$this->inputs = $inputFilter->getInputs();

return $this;
}

Expand All @@ -94,6 +79,7 @@ public function getInputFilter()
if (null === $this->inputFilter) {
$this->setInputFilter(new InputFilter());
}

return $this->inputFilter;
}

Expand All @@ -106,6 +92,7 @@ public function getInputFilter()
public function setIsRequired($isRequired)
{
$this->isRequired = $isRequired;

return $this;
}

Expand All @@ -119,7 +106,6 @@ public function getIsRequired()
return $this->isRequired;
}


/**
* Set the count of data to validate
*
Expand All @@ -129,6 +115,7 @@ public function getIsRequired()
public function setCount($count)
{
$this->count = $count > 0 ? $count : 0;

return $this;
}

Expand All @@ -140,8 +127,9 @@ public function setCount($count)
public function getCount()
{
if (null === $this->count) {
$this->count = count($this->collectionData);
$this->count = count($this->data);
}

return $this->count;
}

Expand All @@ -150,14 +138,15 @@ public function getCount()
*/
public function setData($data)
{
$this->collectionData = $data;
$this->data = $data;
}

/**
* {@inheritdoc}
*/
public function isValid()
{
$inputFilter = $this->getInputFilter();
$valid = true;

if ($this->getCount() < 1) {
Expand All @@ -166,55 +155,37 @@ public function isValid()
}
}

if (count($this->collectionData) < $this->getCount()) {
if (count($this->data) < $this->getCount()) {
$valid = false;
}

if (empty($this->collectionData)) {
if (empty($this->data)) {
$this->clearValues();
$this->clearRawValues();

return $valid;
}

$inputs = $this->validationGroup ?: array_keys($this->inputs);
foreach ($this->collectionData as $key => $data) {
foreach ($this->data as $key => $data) {
if (!is_array($data)) {
$data = array();
}
$this->data = $data;
$this->populate();
$inputFilter->setData($data);

if ($this->validateInputs($inputs, $data)) {
$this->collectionValidInputs[$key] = $this->validInputs;
} else {
$this->collectionInvalidInputs[$key] = $this->invalidInputs;
$valid = false;
if (null !== $this->validationGroup) {
$inputFilter->setValidationGroup($this->validationGroup[$key]);
}

$values = array();
$rawValues = array();
$messages = array();
foreach ($inputs as $name) {
$input = $this->inputs[$name];

if ($input instanceof InputFilterInterface) {
$values[$name] = $input->getValues();
$rawValues[$name] = $input->getRawValues();
continue;
}
$values[$name] = $input->getValue($this->data);
$rawValues[$name] = $input->getRawValue();
$tmpMessages = $input->getMessages();
if (!empty($tmpMessages)) {
$messages[$name] = $tmpMessages;
}
if ($inputFilter->isValid()) {
$this->validInputs[$key] = $inputFilter->getValidInput();
} else {
$valid = false;
$this->collectionMessages[$key] = $inputFilter->getMessages();
$this->invalidInputs[$key] = $inputFilter->getInvalidInput();
}
$this->collectionValues[$key] = $values;
$this->collectionRawValues[$key] = $rawValues;

if (!empty($messages)) {
$this->collectionMessages[$key] = $messages;
}
$this->collectionValues[$key] = $inputFilter->getValues();
$this->collectionRawValues[$key] = $inputFilter->getRawValues();
}

return $valid;
Expand All @@ -227,34 +198,13 @@ public function setValidationGroup($name)
{
if ($name === self::VALIDATE_ALL) {
$this->validationGroup = null;
return $this;
}

if (is_array($name)) {
// Best effort check if the validation group was set by a form for BC
if (count($name) == count($this->collectionData) && is_array(reset($name))) {
return parent::setValidationGroup(reset($name));
}
return parent::setValidationGroup($name);
return $this;
}

return parent::setValidationGroup(func_get_args());
}

/**
* {@inheritdoc}
*/
public function getInvalidInput()
{
return (is_array($this->collectionInvalidInputs) ? $this->collectionInvalidInputs : array());
}
$this->validationGroup = $name;

/**
* {@inheritdoc}
*/
public function getValidInput()
{
return (is_array($this->collectionValidInputs) ? $this->collectionValidInputs : array());
return $this;
}

/**
Expand Down
27 changes: 14 additions & 13 deletions test/CollectionInputFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,6 @@ public function testSetInputFilter()
$this->assertInstanceOf('Zend\InputFilter\BaseInputFilter', $this->filter->getInputFilter());
}

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

$this->filter->setInputFilter($this->getBaseInputFilter());

$this->assertCount(4, $this->filter->getInputs());
}

public function testGetDefaultInputFilter()
{
$this->assertInstanceOf('Zend\InputFilter\BaseInputFilter', $this->filter->getInputFilter());
Expand Down Expand Up @@ -338,26 +327,38 @@ public function testGetMessagesForInvalidInputs()
'baz' => '',
),
),
array(
'foo' => ' bazbat ',
'bar' => '12345',
'baz' => '',
'nest' => array(
// missing 'foo' here
'bar' => '12345',
'baz' => '',
),
),
);

$this->filter->setInputFilter($this->getBaseInputFilter());
$this->filter->setData($invalidCollectionData);

$this->assertFalse($this->filter->isValid());

$this->assertCount(2, $this->filter->getInvalidInput());
$this->assertCount(3, $this->filter->getInvalidInput());
foreach ($this->filter->getInvalidInput() as $invalidInputs) {
$this->assertCount(1, $invalidInputs);
}

$messages = $this->filter->getMessages();

$this->assertCount(2, $messages);
$this->assertCount(3, $messages);
$this->assertArrayHasKey('foo', $messages[0]);
$this->assertArrayHasKey('bar', $messages[1]);
$this->assertArrayHasKey('nest', $messages[2]);

$this->assertCount(1, $messages[0]['foo']);
$this->assertCount(1, $messages[1]['bar']);
$this->assertCount(1, $messages[2]['nest']);
}

public function testSetValidationGroupUsingFormStyle()
Expand Down
8 changes: 6 additions & 2 deletions test/InputFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,13 @@ public function testGetValueReturnsArrayIfNestedInputFilters()
*/
public function testCountZeroValidateInternalInputWithCollectionInputFilter()
{
$inputFilter = new InputFilter();
$inputFilter->add(new Input(), 'name');

$collection = new CollectionInputFilter();
$collection->setCount(0)
->add(new Input(), 'name');
$collection->setInputFilter($inputFilter);
$collection->setCount(0);

$this->filter->add($collection, 'people');

$data = array(
Expand Down

0 comments on commit 0cd91e3

Please sign in to comment.