How to set the order of Zend form elements and avoid duplicates

In a Zend form, if two elements have the same order, Zend completely ignores the second element (instead of displaying it under the first). Take the following code as an example. Note that City and Zip Code elements are in the same order 4

  $address = new Zend_Form_Element_Textarea('address'); $address->setLabel('Address') ->setAttrib('cols', 20) ->setAttrib('rows', 2) ->setOrder(3) ; $city = new Zend_Form_Element_Text('city'); $city->setLabel('City') ->setOrder(4) ; $postal = new Zend_Form_Element_Text('postal'); $postal->setLabel('Zip Code') ->setOrder(4); 

When this form is displayed, the Zip Code element will not be found anywhere.

If I want to dynamically set elements like buttons, but tell them to appear at the end of the form, how would I do this and not run into the problem of having two elements in the same order?

 public function addSubmitButton($label = "Submit", $order = null) { $form_name = $this->getName(); // Convert Label to a lowercase no spaces handle $handle = strtolower(str_replace(" ","_",$label)); $submit = new Zend_Form_Element_Submit($handle); $submit->setLabel($label) ->setAttrib('id', $form_name . "_" . $handle) ; ///////// Set the button order to be at the end of the form ///////// $submit->setOrder(??????); $this->addElement($submit); } 
+4
source share
4 answers

If you really need to use the setOrder () method, I will work with order numbers 10, 20, 30, 40, ... Thus, it will be easy to add elements between already installed elements.

In addition, to avoid using order numbers twice, you can use an array in which you store all numbers from 1 to X. Whenever you set an order number, you set it using the getOrderNumberFromArray () method, which returns the next higher number or lower order, still available in the array, and disables that element of the array.

Alternatively, or maybe even better, you can do getOrder () for the element you want to have before the new element, then increment that serial number by X, and then scroll through existing form elements and verify that the order number is not working Not yet.

Or you can simply use getOrder () on the element that you want to show before and after the new element, and make sure that you are not using the same sequence numbers for the new element.

+7
source

Sorry to be late for the question. I made the Zend_Form extension and override the _sort () method as follows:

 /** * Sort items according to their order * * @return void */ protected function _sort() { if ($this->_orderUpdated) { $items = array(); $index = 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[$order][]= $key; } } ksort($items); $index = 0; foreach($items as $i=>$item){ foreach($item as $subItem){ $newItems[$index++]=$subItem; } } $items = array_flip($newItems); asort($items); $this->_order = $items; $this->_orderUpdated = false; } } 

This differs from the original sorting method by placing items in an array based on their index, and then doing a depth traversal to align the array.

+3
source

Try this code:

 $elements = array(); $elements[] = new Zend_Form_Element_Textarea('address'); ...... $elements[] = new Zend_Form_Element_Text('city'); ....... $elements[] = new Zend_Form_Element_Submit($handle); ..... $this->addElements($elements); 

All you have to do is add them in the order you want them to show

0
source

what would I do - use a temporary array for this - in which the names of the elements are stored in the correct order (do not pay attention to the keys). Then use foreach as follows:

 foreach(array_values($tempArray) as $order => $name) { $form->$name->setOrder($order+1); } 

Pay attention to array_values โ€‹โ€‹- it will return values โ€‹โ€‹as a numbered array;) Not sure if setOrder (0) works - why +1 exists

0
source

Source: https://habr.com/ru/post/1345156/


All Articles