Issues

ZF-9401: Zend_Form numerical names

Description

Zend_Form will not guess the numerical names we give to Forms and Elements and hardcode it in, so there is really no need for this restriction.


class NumericalMembers
{
    public $numerical = 0;

    public function __construct($value)
    {
        $this->{$this->numerical} = $value;
        var_dump($this->{$this->numerical});
    }
}
$n = new NumericalMembers('you See');
// string(7) "you See"

There are several comparisons whithin Zend_Form* which prevent numerical names for Subitems of Forms.

This fix will clean the comparisons in a way that it only checks against appropriate empty Values, empty in a sense of !==null && !==''.

This is the patch for the class Zend_Form, Unit Tests included


Index: tests/Zend/Form/FormTest.php
===================================================================
--- tests/Zend/Form/FormTest.php    (Revision 21857)
+++ tests/Zend/Form/FormTest.php    (Arbeitskopie)
@@ -1579,7 +1579,69 @@
         $this->assertSame($this->form->getValidValues($data['invalid']), $data['partial']);
     }
 
+    public function _setup9401()
+    {
+        $sub0 = 0;
+        $this->form->addSubForm(new Zend_Form_SubForm(), $sub0)
+                   ->$sub0->setElementsBelongTo('f[2]')
+                          ->addElement('text', 'foo')
+                          ->foo->addValidator('Identical',
+                                              false,
+                                              array('foo Value'));
 
+        $this->form->$sub0->addSubForm(new Zend_Form_SubForm(), $sub0)
+                          ->$sub0->addElement('text', 'quo')
+                                 ->quo->addValidator('Identical',
+                                                     false,
+                                                     array('quo Value'));
+
+        $data = array('valid' => array('f' =>
+                                       array(2 =>
+                                             array('foo' => 'foo Value',
+                                                   0 =>
+                                                   array('quo' => 'quo Value')))),
+                      'invalid' => array('f' =>
+                                         array(2 =>
+                                               array('foo' => 'foo Invalid',
+                                                     0 =>
+                                                     array('quo' => 'quo Value')))),
+                      'partial' => array('f' =>
+                                         array(2 =>
+                                               array(0 =>
+                                                     array('quo' => 'quo Value')))));
+        return $data;                      
+    }
+    
+    public function testGetErrorsNumericalSubForms()
+    {
+        $data = $this->_setup9401();
+        $this->form->isValid($data['invalid']);
+        $err = $this->form->getErrors();
+        $this->assertTrue(is_array($err['f'][2]['foo']) && !empty($err['f'][2]['foo']));
+    }
+    
+    public function testGetMessagesNumericalSubForms()
+    {
+        $data = $this->_setup9401();
+        $this->form->isValid($data['invalid']);
+        $msg = $this->form->getMessages();
+        $this->assertTrue(is_array($msg['f'][2]['foo']) && !empty($msg['f'][2]['foo']));
+    }
+
+    public function testGetValuesNumericalSubForms()
+    {
+        $data = $this->_setup9401();
+        $this->form->populate($data['valid']);
+        $this->assertEquals($this->form->getValues(), $data['valid']);
+    }
+
+    public function testGetValidValuesNumericalSubForms()
+    {
+        $data = $this->_setup9401();
+        $this->assertEquals($this->form->getValidValues($data['invalid']), $data['partial']);
+    }
+
+
     // Display groups
 
     public function testCanAddAndRetrieveSingleDisplayGroups()
Index: library/Zend/Form.php
===================================================================
--- library/Zend/Form.php   (Revision 21857)
+++ library/Zend/Form.php   (Arbeitskopie)
@@ -842,7 +842,7 @@
     public function setName($name)
     {
         $name = $this->filterName($name);
-        if (('0' !== $name) && empty($name)) {
+        if ('' === (string)$name) {
             require_once 'Zend/Form/Exception.php';
             throw new Zend_Form_Exception('Invalid name provided; must contain only valid variable characters and be non-empty');
         }
@@ -1416,7 +1416,7 @@
     {
         $origName = $this->getElementsBelongTo();
         $name = $this->filterName($array, true);
-        if (empty($name)) {
+        if ('' === $name) {
             $name = null;
         }
         $this->_elementsBelongTo = $name;
@@ -1468,7 +1468,7 @@
     {
         if ((null === $this->_elementsBelongTo) && $this->isArray()) {
             $name = $this->getName();
-            if (!empty($name)) {
+            if ('' !== (string)$name) {
                 return $name;
             }
         }
@@ -1761,7 +1761,7 @@
     {
         if (null === $name) {
             $name = $group->getName();
-            if (empty($name)) {
+            if ('' === (string)$name) {
                 require_once 'Zend/Form/Exception.php';
                 throw new Zend_Form_Exception('Invalid display group added; requires name');
             }
@@ -1953,7 +1953,7 @@
      */
     protected function _getArrayName($value)
     {
-        if (empty($value) || !is_string($value)) {
+        if (!is_string($value) || '' === $value) {
             return $value;
         }
 

Comments

added Patch including Unit Tests

Patch applied to trunk and 1.10 release branch