HTML_QuickForm bug
| From: | Nigel Armstrong | Date: | Wed, 18 Sep 2002 15:29:41 +0000 |
| Subject: | HTML_QuickForm bug | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-2341@lists.php.net to get a copy of this message | ||
Hi,
There is a bug in the addElement() method that is only apparent when you use
removeElement() as well.
Here's the code (v 1.27) which has the problem, with line numbers added for
clairity :-
573 $index = count($this->_elements);
574 $this->_elementIndex[$elementName] = $index;
575 $this->_elements[] = $elementObject;
576 return $index;
The index is being calculated on the basis of the count of the number of
elements at 573. But if you have previously removed an element from the
_elements array then the actual index allocated at 575 will be out by one.
For example, if I have added three elements 'a', 'b' and 'c' they will
get
indexes 0, 1, and 2 respectively. If I then remove 'b' the array has 2 entries
with indexes 0 and 2. If I then insert another element 'd' its index is
recorded as '2' at line 574 but it actually gets an index of 3 at line 575.
I would suggest the following change, using a class variable _nextElementIndex,
initialized to 0.
573 $index = _nextElementIndex++;
lines 1679-1680 (in freeze()) also needs to be changed, from:-
1679 for ($i=0; $i<count($this->_elements); $i++) {
1680 $element = &$this->_elements[$i];
to
1679 foreach($this->_elements as $element) {
Please note I haven't tested any of this!!
Nigel Armstrong
Tertio Service Management Solutions Limited, 338-346 Goswell Road, London EC1V
7LQ
Tel: 020 7239 0600 Fax: 020 7239 0500 Email: info@tertio.com Web:
http://www.sms-tertio.com
Any views expressed in this message are those of the individual sender,
except where the sender specifically states them to be the views of Tertio
Service Management Solutions Ltd.