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

Commit fc2e1e2

Browse files
committed
Fix missing optional fields should not be validated
Prevent the assumption of `null` value to be a synonym of value not set. Null values are valid input values (like JSON object with null values)
1 parent b75c2a7 commit fc2e1e2

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

CHANGELOG.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,29 @@ All notable changes to this project will be documented in this file, in reverse
1818

1919
### Fixed
2020

21-
- Nothing.
21+
- [#25](https://github.com/zendframework/zend-inputfilter/pull/25) Fix missing optional fields to be required.
22+
BC Break since 2.3.9.
23+
For completely fix this you need to setup your inputs as follow.
24+
25+
```php
26+
$input = new Input();
27+
$input->setAllowEmpty(true); // Disable BC Break logic related to treat `null` values as valid empty value instead *not set*.
28+
$input->setContinueIfEmpty(true); // Disable BC Break logic related to treat `null` values as valid empty value instead *not set*.
29+
$input->getValidatorChain()->attach(new Zend\Validator\NotEmpty(), /* break chain on failure */ true);
30+
```
31+
32+
```php
33+
$inputSpecification = [
34+
'allow_empty' => true,
35+
'continue_if_empty' => true,
36+
'validators' => [
37+
[
38+
'break_chain_on_failure' => true,
39+
'name' => 'Zend\\Validator\\NotEmpty',
40+
],
41+
],
42+
];
43+
```
2244

2345
## 2.5.4 - 2015-08-11
2446

src/BaseInputFilter.php

+7
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,13 @@ protected function validateInputs(array $inputs, $data = [], $context = null)
244244
$input = $this->inputs[$name];
245245
$hasFallback = ($input instanceof Input && $input->hasFallback());
246246

247+
if (!array_key_exists($name, $data)
248+
&& $input instanceof InputInterface
249+
&& !$input->isRequired()
250+
) {
251+
continue;
252+
}
253+
247254
// If the value is required, not present in the data set, and
248255
// has no fallback, validation fails.
249256
if (!array_key_exists($name, $data)

test/BaseInputFilterTest.php

+33
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
namespace ZendTest\InputFilter;
1111

1212
use ArrayObject;
13+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1314
use PHPUnit_Framework_TestCase as TestCase;
1415
use stdClass;
1516
use Zend\InputFilter\Input;
17+
use Zend\InputFilter\InputInterface;
1618
use Zend\InputFilter\FileInput;
1719
use Zend\InputFilter\BaseInputFilter as InputFilter;
1820
use Zend\Filter;
@@ -947,6 +949,37 @@ public function testAllowEmptyTestsFilteredValueAndOverrulesValidatorChain()
947949
$this->assertEquals(['foo' => ''], $filter->getValues());
948950
}
949951

952+
public function testMissingOptionalInputShouldMarkInputAsValid()
953+
{
954+
$optionalInputName = 'fooOptionalInput';
955+
/** @var InputInterface|MockObject $optionalInput */
956+
$optionalInput = $this->getMock(InputInterface::class);
957+
$optionalInput->method('getName')
958+
->willReturn($optionalInputName)
959+
;
960+
$optionalInput->expects($this->never())
961+
->method('isValid')
962+
;
963+
$data = [];
964+
965+
$filter = new InputFilter();
966+
$filter->add($optionalInput);
967+
968+
$filter->setData($data);
969+
970+
$this->assertTrue($filter->isValid(), json_encode($filter->getMessages()));
971+
$this->assertArrayNotHasKey(
972+
$optionalInputName,
973+
$filter->getValidInput(),
974+
'Missing optional fields must not appear as valid input neither invalid input'
975+
);
976+
$this->assertArrayNotHasKey(
977+
$optionalInputName,
978+
$filter->getInvalidInput(),
979+
'Missing optional fields must not appear as valid input neither invalid input'
980+
);
981+
}
982+
950983
public function testAllowEmptyTestsFilteredValueAndContinuesIfEmpty()
951984
{
952985
$input = new Input('foo');

0 commit comments

Comments
 (0)