QuickForm bug and patch, on removeElement()

From: Date: Wed, 16 Jul 2003 08:52:37 +0000
Subject: QuickForm bug and patch, on removeElement()
Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-18285@lists.php.net to get a copy of this message
QuickForm has a bug in the removeElement. When I remove an element which is not at the end, then the elementIndex is updated, but not renumbered, so when i add another element at the end, addElement() obviously uses count or sizeof to determine the the index of this new element Example: remove user[password1] the index before removing: Array (
    [action_save] => 0
    [action_cancel] => 1
    [user[email]] => 2
    [user[name]] => 3
    [user[password1]] => 4
    [user[password]] => 5
    [user[id]] => 6
) the index AFTER removing: Array (
    [action_save] => 0
    [action_cancel] => 1
    [user[email]] => 2
    [user[name]] => 3
    [user[password]] => 5
    [user[id]] => 6
) here i add an element (with the same name - which is not important) remove user[password] the index before removing: Array (
    [action_save] => 0
    [action_cancel] => 1
    [user[email]] => 2
    [user[name]] => 3
    [user[password]] => 5
    [user[id]] => 6
    [user[password1]] => 6
) the index AFTER removing: Array (
    [action_save] => 0
    [action_cancel] => 1
    [user[email]] => 2
    [user[name]] => 3
    [user[id]] => 6
    [user[password1]] => 6
) this patch solves it, at least for me. it might not be the coolest code to do that, but i guess you QuickForm guys have a better solution at hand within seconds RCS file: /repository/pear/HTML_QuickForm/QuickForm.php,v retrieving revision 1.108 diff -u -r1.108 QuickForm.php
--- QuickForm.php       28 Jun 2003 10:58:49 -0000      1.108
+++ QuickForm.php       16 Jul 2003 08:46:02 -0000
@@ -819,6 +819,13 @@
         if (isset($this->_elementIndex[$elementName])) {
             unset($this->_elements[$this->_elementIndex[$elementName]]);
             unset($this->_elementIndex[$elementName]);
+            // renumber the elementIndex array, since the element that was removed might has left
+            // a gap, which would lead to wrong numbering when adding new elements
+            $oldElIndex = $this->_elementIndex;
+            $this->_elementIndex = array();
+            foreach ($oldElIndex as $k=>$v) {
+                $this->_elementIndex[$k] = sizeof($this->_elementIndex);
+            }
             if ($removeRules) {
                 unset($this->_rules[$elementName]);
             }
thanks -- Wolfram ... opensource @ vision:produktion ... http://opensource.visionp.de ... authentication system .... http://sf.net/projects/auth

« previous php.pear.dev (#18285) next »