Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
wants to merge 1 commit into from

2 participants

@adamlundrigan

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

svn sync r24197 r24552 (trunk)

@adamlundrigan adamlundrigan [hotfix/ZF-11402] Zend\Form\Element
Zend\Form\Element\MultiCheckbox does not accept empty array even when AllowEmpty flag is set
7504849
@weierophinney

Reviewed, merged, and pushed to master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 4, 2011
  1. @adamlundrigan

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

    adamlundrigan authored
    Zend\Form\Element\MultiCheckbox does not accept empty array even when AllowEmpty flag is set
This page is out of date. Refresh to see the latest.
View
11 library/Zend/Form/Element.php
@@ -1373,7 +1373,14 @@ public function isValid($value, $context = null)
if ($isArray && is_array($value)) {
$messages = array();
$errors = array();
- foreach ($value as $val) {
+ if (empty($value)) {
+ if ($this->isRequired()
+ || (!$this->isRequired() && !$this->getAllowEmpty())
+ ) {
+ $value = '';
+ }
+ }
+ foreach ((array)$value as $val) {
if (!$validator->isValid($val, $context)) {
$result = false;
if ($this->_hasErrorMessages()) {
@@ -1381,7 +1388,7 @@ public function isValid($value, $context = null)
$errors = $messages;
} else {
$messages = array_merge($messages, $validator->getMessages());
- $errors = array_merge($errors, $validator->getErrors());
+ $errors = $messages;
}
}
}
View
39 tests/Zend/Form/Element/MultiCheckboxTest.php
@@ -250,4 +250,43 @@ public function testRetrievingErrorMessagesShouldNotResultInError()
$this->element->isValid(array('foo', 'bogus'));
$html = $this->element->render($this->getView());
}
+
+ /**
+ * @group ZF-11402
+ */
+ public function testValidateShouldNotAcceptEmptyArray()
+ {
+ $this->element->addMultiOptions(array(
+ 'foo' => 'Foo',
+ 'bar' => 'Bar',
+ 'baz' => 'Baz',
+ ));
+ $this->element->setRegisterInArrayValidator(true);
+
+ $this->assertTrue($this->element->isValid(array('foo')));
+ $this->assertTrue($this->element->isValid(array('foo','baz')));
+
+ $this->element->setAllowEmpty(true);
+ $this->assertTrue($this->element->isValid(array()));
+
+ // Empty value + AllowEmpty=true = no error messages
+ $messages = $this->element->getMessages();
+ $this->assertEquals(0, count($messages), 'Received unexpected error message(s)');
+
+ $this->element->setAllowEmpty(false);
+ $this->assertFalse($this->element->isValid(array()));
+
+ // Empty value + AllowEmpty=false = notInArray error message
+ $messages = $this->element->getMessages();
+ $this->assertTrue(is_array($messages), 'Expected error message');
+ $this->assertArrayHasKey('notInArray', $messages, 'Expected \'notInArray\' error message');
+
+ $this->element->setRequired(true)->setAllowEmpty(false);
+ $this->assertFalse($this->element->isValid(array()));
+
+ // Empty value + Required=true + AllowEmpty=false = isEmpty error message
+ $messages = $this->element->getMessages();
+ $this->assertTrue(is_array($messages), 'Expected error message');
+ $this->assertArrayHasKey('isEmpty', $messages, 'Expected \'isEmpty\' error message');
+ }
}
Something went wrong with that request. Please try again.