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

Commit

Permalink
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/CollectionInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class CollectionInputFilter extends InputFilter
*/
protected $collectionInvalidInputs;

/*
* @var bool
*/
protected $isRequired = false;

/*
* @var int
*/
Expand Down Expand Up @@ -87,6 +92,29 @@ public function getInputFilter()
return $this->inputFilter;
}

/**
* Set if the collection can be empty
*
* @param bool $isRequired
* @return CollectionInputFilter
*/
public function setIsRequired($isRequired)
{
$this->isRequired = $isRequired;
return $this;
}

/**
* Get if collection can be empty
*
* @return bool
*/
public function getIsRequired()
{
return $this->isRequired;
}


/**
* Set the count of data to validate
*
Expand Down Expand Up @@ -128,6 +156,9 @@ public function isValid()
$valid = true;

if ($this->getCount() < 1) {
if ($this->isRequired) {
$valid = false;
}
return $valid;
}

Expand Down
27 changes: 27 additions & 0 deletions test/CollectionInputFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,31 @@ public function testSetValidationGroupUsingFormStyle()

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

public function testEmptyCollectionIsValidByDefault()
{
$data = array();

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

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

public function testEmptyCollectionIsNotValidIfRequired()
{
$data = array();

$this->filter->setInputFilter($this->getBaseInputFilter());
$this->filter->setData($data);
$this->filter->setIsRequired(true);

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

public function testSetRequired()
{
$this->filter->setIsRequired(true);
$this->assertEquals(true,$this->filter->getIsRequired());
}
}

0 comments on commit f9afa8f

Please sign in to comment.