简体   繁体   中英

post multidimensional array with Zend Form

i created a zend form which is able to generate subform with the elements like: items[0][price], items[0][count]; items[1][price] etc. in each row of a table i need to display certain data. so, in my view i created a html table and tried to iterate through the 'items' and insert certain form element in certain column of a table.

<table>
<?php foreach($this->form->getSubform('items') as $item) :?>
  <tr>
    <td>just some data like product name</td>
    <td><?php echo $item->getElement('price');?></td>
    <td><?php echo $item->getElement('count');?></td>
  </tr>
<?php endforeach;?>
</table>

after that, each form element item do NOT have array name anymore, as i was expecting, but just like 'items' or 'count' in each row, so multidimensional array will not be returned.

if i generate the form in view ( echo $this->form; ), then form element names are ok, but how should i add some data in the table then..? preferably it should be done in view.

Instead of adding elements always with price and count, since you're generating the field dinamically, try to name them price1, count1, price2, count2, and so on. Then you can retrieve them in your table using their new name (with the count appended).

Prepend your form decorator stack with the PrepareElements decorator. This decorator will loop through your form and set the appropriate belongsTo names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM