Re: HTML_QuickForm bug

From: Date: Wed, 18 Sep 2002 18:39:25 +0000
Subject: Re: HTML_QuickForm bug
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-2346@lists.php.net to get a copy of this message
I'm not positive but I think the change you proposed in freeze() lines 1679-1680 might not work. The regular for loop is being used so $element will contain the address of the array value at $i. That way when $element is changed it changes the variable in the array directly. In the foreach $element will contain a copy of the variable and will not produce the same results. Adam > 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) { > > > > > 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. > > > > -- > PEAR General Mailing List (http://pear.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php --------------------------------- Adam Morton Developer - Web Technology Group Appalachian State University http://phpwebsite.appstate.edu adam@tux.appstate.edu

« previous php.pear.general (#2346) next »