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 5 changed files with 67 additions and 10 deletions.
6 changes: 6 additions & 0 deletions composer.json
Expand Up @@ -17,6 +17,12 @@
"zendframework/zend-validator": "self.version",
"zendframework/zend-stdlib": "self.version"
},
"extra": {
"branch-alias": {
"dev-master": "2.1-dev",
"dev-develop": "2.2-dev"
}
},
"homepage": "https://github.com/zendframework/zend-input-filter",
"autoload-dev": {
"psr-4": {
Expand Down
7 changes: 5 additions & 2 deletions src/BaseInputFilter.php
Expand Up @@ -171,7 +171,10 @@ public function isValid()
) {
if ($input instanceof InputInterface) {
// - test if input is required
if (!$input->isRequired()) {
if (!$input->isRequired()
// "Not required" should not apply to empty strings (#3983)
&& !(array_key_exists($name, $this->data) && is_string($this->data[$name]))
) {
$this->validInputs[$name] = $input;
continue;
}
Expand All @@ -182,7 +185,7 @@ public function isValid()
}
}
// make sure we have a value (empty) for validation
$this->data[$name] = '';
$this->data[$name] = null;
}

if ($input instanceof InputFilterInterface) {
Expand Down
3 changes: 3 additions & 0 deletions src/Factory.php
Expand Up @@ -153,6 +153,9 @@ public function createInput($inputSpecification)
$input->setRequired(!$value);
}
break;
case 'error_message':
$input->setErrorMessage($value);
break;
case 'fallback_value':
$input->setFallbackValue($value);
break;
Expand Down
51 changes: 43 additions & 8 deletions test/BaseInputFilterTest.php
Expand Up @@ -85,9 +85,15 @@ public function getInputFilter()
$baz->getFilterChain()->attachByName('stringtrim');
$baz->getValidatorChain()->attach(new Validator\StringLength(1, 6));

$qux = new Input();
$qux->setAllowEmpty(true);
$qux->getFilterChain()->attachByName('stringtrim');
$qux->getValidatorChain()->attach(new Validator\StringLength(5, 6));

$filter->add($foo, 'foo')
->add($bar, 'bar')
->add($baz, 'baz')
->add($qux, 'qux')
->add($this->getChildInputFilter(), 'nest');

return $filter;
Expand Down Expand Up @@ -123,20 +129,48 @@ public function testCanValidateEntireDataset()
$validData = array(
'foo' => ' bazbat ',
'bar' => '12345',
'baz' => '',
'baz' => null,
'qux' => '',
'nest' => array(
'foo' => ' bazbat ',
'bar' => '12345',
'baz' => '',
'baz' => null,
),
);
$filter->setData($validData);
$this->assertTrue($filter->isValid());

$filter = $this->getInputFilter();
$validData = array(
'foo' => ' bazbat ',
'bar' => '12345',
'qux' => '',
'nest' => array(
'foo' => ' bazbat ',
'bar' => '12345',
),
);
$filter->setData($validData);
$this->assertTrue($filter->isValid());

$invalidData = array(
'foo' => ' bazbat ',
'bar' => '12345',
'baz' => '',
'nest' => array(
'foo' => ' bazbat ',
'bar' => '12345',
'baz' => '',
),
);
$filter->setData($invalidData);
$this->assertFalse($filter->isValid());

$invalidData = array(
'foo' => ' baz bat ',
'bar' => 'abc45',
'baz' => ' ',
'qux' => ' ',
'nest' => array(
'foo' => ' baz bat ',
'bar' => '123ab',
Expand Down Expand Up @@ -227,23 +261,23 @@ public function testValuesRetrievedAreFiltered()
$validData = array(
'foo' => ' bazbat ',
'bar' => '12345',
'baz' => '',
'qux' => '',
'nest' => array(
'foo' => ' bazbat ',
'bar' => '12345',
'baz' => '',
),
);
$filter->setData($validData);
$this->assertTrue($filter->isValid());
$expected = array(
'foo' => 'bazbat',
'bar' => '12345',
'baz' => '',
'baz' => null,
'qux' => '',
'nest' => array(
'foo' => 'bazbat',
'bar' => '12345',
'baz' => '',
'baz' => null,
),
);
$this->assertEquals($expected, $filter->getValues());
Expand All @@ -255,11 +289,12 @@ public function testCanGetRawInputValues()
$validData = array(
'foo' => ' bazbat ',
'bar' => '12345',
'baz' => '',
'baz' => null,
'qux' => '',
'nest' => array(
'foo' => ' bazbat ',
'bar' => '12345',
'baz' => '',
'baz' => null,
),
);
$filter->setData($validData);
Expand Down
10 changes: 10 additions & 0 deletions test/FactoryTest.php
Expand Up @@ -413,4 +413,14 @@ public function testFactoryAllowsPassingFilterChainsInInputSpec()
$test = $input->getFilterChain();
$this->assertSame($chain, $test);
}

public function testFactoryWillCreateInputWithErrorMessage()
{
$factory = new Factory();
$input = $factory->createInput(array(
'name' => 'foo',
'error_message' => 'My custom error message',
));
$this->assertEquals('My custom error message', $input->getErrorMessage());
}
}

0 comments on commit 45c4355

Please sign in to comment.