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

Commit

Permalink
[#7] Ensure Required, AllowEmpty validation combo works
Browse files Browse the repository at this point in the history
Per the issue, this patch ensures that if an input is marked as required
**and** marked to allow empty that the following works as expected:

- If the input name is present in the data set, but a value that
  evaluates to empty, it will validate as true.
- If the input name **is not** present in the data set, it will validate
  as false.
  • Loading branch information
weierophinney committed Jul 23, 2015
1 parent 9e8f20f commit 3575ece
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/BaseInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,15 @@ public function setData($data)
*/
public function isValid($context = null)
{
$data = $this->getRawValues();
if (null === $data) {
if (null === $this->data) {
throw new Exception\RuntimeException(sprintf(
'%s: no data present to validate!',
__METHOD__
));
}

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

/**
Expand All @@ -244,7 +243,24 @@ protected function validateInputs(array $inputs, array $data = [], $context = nu
foreach ($inputs as $name) {
$input = $this->inputs[$name];

// make sure we have a value (empty) for validation of context
// If the value is required, but not present in the data set,
// validation fails.
if (!array_key_exists($name, $data)
&& $input instanceof InputInterface
&& $input->isRequired()
) {
$input->setErrorMessage('Value is required');
$this->invalidInputs[$name] = $input;

if ($input->breakOnFailure()) {
return false;
}

$valid = false;
continue;
}

// Make sure we have a value (empty) for validation of context
if (!array_key_exists($name, $data)) {
$data[$name] = null;
}
Expand Down
56 changes: 56 additions & 0 deletions test/BaseInputFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -641,8 +641,10 @@ public static function contextDataProvider()
/**
* @dataProvider contextDataProvider()
*/
// @codingStandardsIgnoreStart
public function testValidationMarksInputValidWhenAllowEmptyFlagIsTrueAndContinueIfEmptyIsTrueAndContextValidatesEmptyField($allowEmpty, $blankIsValid, $valid)
{
// @codingStandardsIgnoreEnd
$filter = new InputFilter();

$data = [
Expand Down Expand Up @@ -964,4 +966,58 @@ public function testAllowEmptyTestsFilteredValueAndContinuesIfEmpty()

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

/**
* @group 7
*/
public function testMissingRequiredAllowedEmptyValueShouldMarkInputFilterInvalid()
{
$foo = new Input('foo');
$foo->setRequired(true);
$foo->setAllowEmpty(false);

$bar = new Input('bar');
$bar->setRequired(true);
$bar->setAllowEmpty(true);

$filter = new InputFilter();
$filter->add($foo);
$filter->add($bar);

$filter->setData(['foo' => 'xyz']);
$this->assertFalse($filter->isValid(), 'Missing required value should mark input filter as invalid');
}

public function emptyValuesForValidation()
{
return [
'null' => [null],
'empty-string' => [''],
];
}

/**
* @group 7
* @dataProvider emptyValuesForValidation
*/
public function testEmptyValuePassedForRequiredButAllowedEmptyInputShouldMarkInputFilterValid($value)
{
$foo = new Input('foo');
$foo->setRequired(true);
$foo->setAllowEmpty(false);

$bar = new Input('bar');
$bar->setRequired(true);
$bar->setAllowEmpty(true);

$filter = new InputFilter();
$filter->add($foo);
$filter->add($bar);

$filter->setData([
'foo' => 'xyz',
'bar' => $value,
]);
$this->assertTrue($filter->isValid(), 'Empty value should mark input filter as valid');
}
}

0 comments on commit 3575ece

Please sign in to comment.