Permalink
Browse files

Merge branch 'hotfix/3983'

Close #3983
  • Loading branch information...
2 parents e00aefc + 6f6cb42 commit 732d8be2fad0649bf0a1c68530c92ed7f6dcda04 @weierophinney weierophinney committed Mar 25, 2013
Showing with 58 additions and 20 deletions.
  1. +9 −6 library/Zend/InputFilter/BaseInputFilter.php
  2. +49 −14 tests/ZendTest/InputFilter/BaseInputFilterTest.php
@@ -159,6 +159,14 @@ public function isValid()
$inputs = $this->validationGroup ?: array_keys($this->inputs);
foreach ($inputs as $name) {
$input = $this->inputs[$name];
+ if ((!array_key_exists($name, $this->data)
+ || (null === $this->data[$name]))
+ && $input instanceof InputInterface
+ && !$input->isRequired()
+ ) {
+ $this->validInputs[$name] = $input;
+ continue;
+ }
if (!array_key_exists($name, $this->data)
|| (null === $this->data[$name])
|| (is_string($this->data[$name]) && strlen($this->data[$name]) === 0)
@@ -170,19 +178,14 @@ public function isValid()
&& isset($this->data[$name][0]['error']) && $this->data[$name][0]['error'] === UPLOAD_ERR_NO_FILE)
) {
if ($input instanceof InputInterface) {
- // - test if input is required
- if (!$input->isRequired()) {
- $this->validInputs[$name] = $input;
- continue;
- }
// - test if input allows empty
if ($input->allowEmpty()) {
$this->validInputs[$name] = $input;
continue;
}
}
// make sure we have a value (empty) for validation
- $this->data[$name] = '';
+ $this->data[$name] = null;
}
if ($input instanceof InputFilterInterface) {
@@ -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;
@@ -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',
@@ -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());
@@ -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);
@@ -415,13 +450,13 @@ public function testValidationSkipsFieldsMarkedNotRequiredWhenNoDataPresent()
$this->assertTrue($filter->isValid());
}
- public function testValidationSkipsFileInputsMarkedNotRequiredWhenNoFileDataIsPresent()
+ public function testValidationSkipsFileInputsMarkedAllowEmptyWhenNoFileDataIsPresent()
{
$filter = new InputFilter();
$foo = new FileInput();
$foo->getValidatorChain()->attach(new Validator\File\UploadFile());
- $foo->setRequired(false);
+ $foo->setAllowEmpty(true);
$filter->add($foo, 'foo');
@@ -438,16 +473,16 @@ public function testValidationSkipsFileInputsMarkedNotRequiredWhenNoFileDataIsPr
$this->assertTrue($filter->isValid());
// Negative test
- $foo->setRequired(true);
+ $foo->setAllowEmpty(false);
$filter->setData($data);
$this->assertFalse($filter->isValid());
}
- public function testValidationSkipsFileInputsMarkedNotRequiredWhenNoMultiFileDataIsPresent()
+ public function testValidationSkipsFileInputsMarkedAllowEmptyWhenNoMultiFileDataIsPresent()
{
$filter = new InputFilter();
$foo = new FileInput();
- $foo->setRequired(false);
+ $foo->setAllowEmpty(true);
$filter->add($foo, 'foo');
$data = array(
@@ -463,7 +498,7 @@ public function testValidationSkipsFileInputsMarkedNotRequiredWhenNoMultiFileDat
$this->assertTrue($filter->isValid());
// Negative test
- $foo->setRequired(true);
+ $foo->setAllowEmpty(false);
$filter->setData($data);
$this->assertFalse($filter->isValid());
}

0 comments on commit 732d8be

Please sign in to comment.