ZF-11744: Setting the same order on a FormElement "deletes" Elements
Description
When you have 2 or more elements with the same order:
$form->addElement(new Zend_Form_Element_Text('one', array('order' => 1)));
$form->addElement(new Zend_Form_Element_Text('two', array('order' => 1)));
The first element doesn't get show, because internally the sort function overwrites it. My solution is to alter the _sort method and let it check for duplicate Order values. If found, add a (couple) of thousands. This is a working solution for up to 1000 duplicate orderV values.
/**
* Sort items according to their order
*
* @return void
*/
protected function _sort()
{
if ($this->_orderUpdated) {
$items = array();
$index = $uniqOrder = 0 ;
foreach ($this->_order as $key => $order) {
if (null === $order) {
if (null === ($order = $this->{$key}->getOrder())) {
while (array_search($index, $this->_order, true)) {
++$index;
}
$items[$index] = $key;
++$index;
} else {
$items[$order] = $key;
}
} else {
$items[isset($items[$order]) ? strval($order + (++$uniqOrder)/1000) : $order] = $key;
}
}
$items = array_flip($items);
asort($items);
$this->_order = $items;
$this->_orderUpdated = false;
}
}
Btw why don't we have a proper coding system? I can put above code into code tags and we still don't have php!! syntax support.
Comments
Posted by Frank Brückner (frosch) on 2011-09-20T14:26:17.000+0000
Code tags added.