Re: QuickForm bug and patch, on removeElement()
| From: | Wolfram Kriesing | Date: | Wed, 16 Jul 2003 09:29:15 +0000 |
| Subject: | Re: QuickForm bug and patch, on removeElement() | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-18288@lists.php.net to get a copy of this message | ||
now i got the right patch, it is actually much easier.
the problem was just when a new element was added, that the _elementIndex was built using count, which is not right when a gap was created, so we have to use the real index used.
this patch does it finally :-)
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 09:27:29 -0000@@ -591,7 +591,8 @@
}
} else {
$this->_elements[] =& $elementObject;
- $this->_elementIndex[$elementName] = count($this->_elements) - 1;
+ $revKeys = array_reverse(array_keys($this->_elements));
+ $this->_elementIndex[$elementName] = $revKeys[0];
}
return $elementObject;Wolfram Kriesing wrote:
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 (-- Wolfram ... opensource @ vision:produktion ... http://opensource.visionp.de ... authentication system .... http://sf.net/projects/auth[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