Zend framework 具有相同索引的数组的zend形式

Zend framework 具有相同索引的数组的zend形式,zend-framework,zend-form,Zend Framework,Zend Form,我需要在zend获得类似的东西 <input type="text" name="phone[1]" value="" /> <input type="text" name="address[1]" value="" /> <input type="text" name="banana[1]" value="whatever" /> 请注意,它们在括号内具有相同的id!(我不需要name=“phone[]”,或name=“phone[phone1]”)

我需要在zend获得类似的东西

<input type="text" name="phone[1]" value="" />
<input type="text" name="address[1]" value="" />
<input type="text" name="banana[1]" value="whatever" />

请注意,它们在括号内具有相同的id!(我不需要
name=“phone[]”
,或
name=“phone[phone1]”

我试了又试 和 及

问题是在ZendFramework中,我不得不用相同的名称“1”命名3个元素,最后一个元素覆盖了前一个元素。即使我创建了3个子表单,我也会得到同样的效果

不同的示例显示了如何获取具有不同索引或没有索引的数组(
[]
),但我需要不同的数组才能具有相同的索引


谢谢

我不明白您为什么需要这种特殊情况,但我认为唯一可能的解决方案是使用自定义模板

class YourForm extends Zend_Form
{
    public function init()
    {
        $this->setDecorators(array(
            array(
                'ViewScript',
                array(
                    'viewScript' => 'path/to/your/phtml/file',
                    'possibleOtherParamYouWantToPass' => 'value',
                    ...
                )
            )
        ));
    }
}
所以你要做的是,你说你想使用一个模板文件,在那里你可以自己声明一切。还有您的
香蕉[1]


但您将失去简单的验证和其他好处。

我不明白,为什么您需要这种特殊情况,但我认为唯一可能的解决方案是使用自定义模板

class YourForm extends Zend_Form
{
    public function init()
    {
        $this->setDecorators(array(
            array(
                'ViewScript',
                array(
                    'viewScript' => 'path/to/your/phtml/file',
                    'possibleOtherParamYouWantToPass' => 'value',
                    ...
                )
            )
        ));
    }
}
所以你要做的是,你说你想使用一个模板文件,在那里你可以自己声明一切。还有您的
香蕉[1]


但是,您将失去简单的验证和其他好处。

Zend_表单具有此名为
setElementsBelongTo
的功能。看见

使用方法是将前缀设置为Zend_Form对象,前缀为
setElementsBelongTo
,如果您希望迭代每个字段,则可以使用子表单封装每组字段

您可以在控制器或表单类的
init()
方法中调用
setElementsBelongTo

$mainForm = new Zend_Form();

$phoneForm = new Zend_Form_Subform();
$element = $phoneForm->createElement('text', '1'); // 1 is the element inside of the brackets
$phoneForm->addElement($element);
$phoneForm->setElementsBelongTo('phone'); // phone is the part leading the brackets
$mainForm->addSubform($phoneForm, 'phone_form');

$phoneForm = new Zend_Form_Subform();
$element = $phoneForm->createElement('text', '2'); // 1 is the element inside of the brackets
$phoneForm->addElement($element);
$phoneForm->setElementsBelongTo('phone'); // phone is the part leading the brackets
$mainForm->addSubform($phoneForm, 'phone_form2');

$addressForm = new Zend_Form_Subform();
$element = $addressForm->createElement('text', '1');
$addressForm->addElement($element);
$addressForm->setElementsBelongTo('address');
$mainForm->addSubform($addressForm, 'address_form');

echo $mainForm;

var_dump($mainForm->getValues());
给予

要获得预期结果,您需要删除一些装饰器(表单、dt等):


Zend_表单为此命名的
setElementsBelongTo
提供了一个功能。看见

使用方法是将前缀设置为Zend_Form对象,前缀为
setElementsBelongTo
,如果您希望迭代每个字段,则可以使用子表单封装每组字段

您可以在控制器或表单类的
init()
方法中调用
setElementsBelongTo

$mainForm = new Zend_Form();

$phoneForm = new Zend_Form_Subform();
$element = $phoneForm->createElement('text', '1'); // 1 is the element inside of the brackets
$phoneForm->addElement($element);
$phoneForm->setElementsBelongTo('phone'); // phone is the part leading the brackets
$mainForm->addSubform($phoneForm, 'phone_form');

$phoneForm = new Zend_Form_Subform();
$element = $phoneForm->createElement('text', '2'); // 1 is the element inside of the brackets
$phoneForm->addElement($element);
$phoneForm->setElementsBelongTo('phone'); // phone is the part leading the brackets
$mainForm->addSubform($phoneForm, 'phone_form2');

$addressForm = new Zend_Form_Subform();
$element = $addressForm->createElement('text', '1');
$addressForm->addElement($element);
$addressForm->setElementsBelongTo('address');
$mainForm->addSubform($addressForm, 'address_form');

echo $mainForm;

var_dump($mainForm->getValues());
给予

要获得预期结果,您需要删除一些装饰器(表单、dt等):


我需要验证和其他好处,这就是挑战的所在我需要验证和其他好处,这就是挑战的所在疯狂,我必须为每个输入创建一个子表单?Max4有很多工具,这是你的选择为你自己的需要选择一个或混合它们。这并不像你所说的那样有效,我得到的不是zend madness,而是我必须为每个输入创建一个子表单?Max4有很多工具,你可以选择根据自己的需要选择一个或混合它们。正如你所说的,我得到的不是
Array(
   'phone' = Array(
       '1' => <value>,
   ),
   'address' = Array(
       '1' => <value>,
   )
);