简体   繁体   中英

How to create a multidimensional array of Zend_Form_Elements

I'm currently having trouble implementing a Zend_Form/SubForm that contains a 2-dimensional array of elements.

I have some data that is returned from a DB query, that I would like to use in an edit form, and I'm looking for my form to output HTML input tags with a multidimensional array notation as a name, such that the data returned upon posting is a 2-dimensional array for easier processing.

Example expected output:

<input type="text" name="data[1][val] value="1">
<input type="text" name="data[1][str] value="asdf">
<input type="text" name="data[2][val] value="2">
<input type="text" name="data[2][str] value="fdsa">

During my trials using subforms, I have been able to accomplish the following output, but my first pair of brackets always seem to be filtered out and I am left with:

<input type="text" name="data1[val] value="1">
<input type="text" name="data1[str] value="asdf">
<input type="text" name="data2[val] value="2">
<input type="text" name="data2[str] value="fdsa">

During my many searches, I have been been able to find a lot of documentation on how to accomplish a 1-dimensional array notation, but am coming up blank regarding 2-dimensional array notation.

Well apperently you have a subform called data1 right? With two elements, the element with name 'val' and another named 'str', try this, not sure if it will work though:

$elementVal->setIsArray(true);

this will add anther dimension to the array 'data1' but i could not specify a name to it, so it would appear like 'data1[val][]'

the setIsArray for the form should be set to true
$subform1->setIsArray(true);
$subform2->setIsArray(true);
and then the setElementsBelongto can be used as
$subform1->setElementsBelongTo('data[1]');
$subform2->setElementsBelongTo('data[2]');
I hope this gives you some idea.

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