Skip to content
This repository

[hotfix/ZF-11402] Zend\Form\Element #562

Closed
wants to merge 1 commit into from

2 participants

Adam Lundrigan Matthew Weier O'Phinney
Adam Lundrigan

Zend\Form\Element\MultiCheckbox does not accept empty array even when AllowEmpty flag is set

svn sync r24197 r24552 (trunk)

Adam Lundrigan [hotfix/ZF-11402] Zend\Form\Element
Zend\Form\Element\MultiCheckbox does not accept empty array even when AllowEmpty flag is set
7504849
Matthew Weier O'Phinney

Reviewed, merged, and pushed to master.

Matthew Weier O'Phinney weierophinney closed this November 10, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Nov 03, 2011
Adam Lundrigan [hotfix/ZF-11402] Zend\Form\Element
Zend\Form\Element\MultiCheckbox does not accept empty array even when AllowEmpty flag is set
7504849
This page is out of date. Refresh to see the latest.
11  library/Zend/Form/Element.php
@@ -1373,7 +1373,14 @@ public function isValid($value, $context = null)
1373 1373
             if ($isArray && is_array($value)) {
1374 1374
                 $messages = array();
1375 1375
                 $errors   = array();
1376  
-                foreach ($value as $val) {
  1376
+                if (empty($value)) {
  1377
+                    if ($this->isRequired()
  1378
+                        || (!$this->isRequired() && !$this->getAllowEmpty())
  1379
+                    ) {
  1380
+                        $value = '';
  1381
+                    }
  1382
+                }
  1383
+                foreach ((array)$value as $val) {
1377 1384
                     if (!$validator->isValid($val, $context)) {
1378 1385
                         $result = false;
1379 1386
                         if ($this->_hasErrorMessages()) {
@@ -1381,7 +1388,7 @@ public function isValid($value, $context = null)
1381 1388
                             $errors   = $messages;
1382 1389
                         } else {
1383 1390
                             $messages = array_merge($messages, $validator->getMessages());
1384  
-                            $errors   = array_merge($errors,   $validator->getErrors());
  1391
+                            $errors   = $messages;
1385 1392
                         }
1386 1393
                     }
1387 1394
                 }
39  tests/Zend/Form/Element/MultiCheckboxTest.php
@@ -250,4 +250,43 @@ public function testRetrievingErrorMessagesShouldNotResultInError()
250 250
         $this->element->isValid(array('foo', 'bogus'));
251 251
         $html = $this->element->render($this->getView());
252 252
     }
  253
+ 
  254
+    /**
  255
+     * @group ZF-11402
  256
+    */
  257
+    public function testValidateShouldNotAcceptEmptyArray()
  258
+    {
  259
+        $this->element->addMultiOptions(array(
  260
+            'foo' => 'Foo',
  261
+            'bar' => 'Bar',
  262
+            'baz' => 'Baz',
  263
+        ));
  264
+        $this->element->setRegisterInArrayValidator(true);
  265
+
  266
+        $this->assertTrue($this->element->isValid(array('foo')));
  267
+        $this->assertTrue($this->element->isValid(array('foo','baz')));
  268
+
  269
+        $this->element->setAllowEmpty(true);
  270
+        $this->assertTrue($this->element->isValid(array()));
  271
+ 
  272
+        // Empty value + AllowEmpty=true = no error messages
  273
+        $messages = $this->element->getMessages();
  274
+        $this->assertEquals(0, count($messages), 'Received unexpected error message(s)');
  275
+
  276
+        $this->element->setAllowEmpty(false);
  277
+        $this->assertFalse($this->element->isValid(array()));
  278
+
  279
+        // Empty value + AllowEmpty=false = notInArray error message
  280
+        $messages = $this->element->getMessages();
  281
+        $this->assertTrue(is_array($messages), 'Expected error message');
  282
+        $this->assertArrayHasKey('notInArray', $messages, 'Expected \'notInArray\' error message');
  283
+
  284
+        $this->element->setRequired(true)->setAllowEmpty(false);
  285
+        $this->assertFalse($this->element->isValid(array()));
  286
+
  287
+        // Empty value + Required=true + AllowEmpty=false = isEmpty error message
  288
+        $messages = $this->element->getMessages();
  289
+        $this->assertTrue(is_array($messages), 'Expected error message');
  290
+        $this->assertArrayHasKey('isEmpty', $messages, 'Expected \'isEmpty\' error message');
  291
+    }
253 292
 }
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.