问题描述
我想使用视图脚本来呈现我的 zend 表单,因为它似乎是最好的方法在仍然使用 Zend_Elements 类的同时控制表单的布局/设计.
I want to use a view script to render my zend form as it seems to be the best way to control the layout/design of the form while still using the Zend_Elements classes.
在视图脚本中,我使用 $this->element->getElement('elementName')
渲染元素.
From the view script, I render the element with $this->element->getElement('elementName')
.
元素名称有问题.这实际上是一个表单中的一个子表单中的一个子表单.
I'm having problems with the names of the elements. This is actually a sub-form inside a sub-form inside a form.
当我使用 FormElements 装饰器时,元素的完全限定名称是 form[subForm][subForm][element] ,这很好.Wehn 我移到 viewScript 装饰器,它变成了 subForm[subForm][element].
When I used the FormElements decorators , the fully qualified name of the elements was form[subForm][subForm][element] , which was good.Wehn I moved to the viewScript decorators, it changed to subForm[subForm][element].
我知道我需要使用 PrepareElements 装饰器来解决这个问题,但这导致名称更改 form[subForm][form][subForm][subForm][elements](它在开始时将前两个名称加倍).
I understood that I need to use the PrepareElements decorator to fix this, but this caused the name to change form[subForm][form][subForm][subForm][elements] (it doubled the first two names in the start).
我应该如何处理这个问题有什么想法吗?
Any ideas how I should handle this?
谢谢.
更新:我尝试调试 PrepareElements,但我真的不明白在做什么.看起来它在第一次迭代中工作正常,但是当在其中一个中间子表单上运行时,它再次添加了 form[subform] 前缀.
UPDATE: I tried to debug PrepareElements and I really don't understand what is doing.It seems like it works ok in the first iteration, but then it adds again the form[subform] prefix when running on one of the middle subforms.
当我不使用 PrepareElements 装饰器时,我只是在名称中缺少form"前缀(即,我只得到 subForm[element] 而不是 form[subForm][element]).
When I'm not using the PrepareElements decorator, I'm just missing the "form" prefix in the names (i.e., instead of form[subForm][element], I get only subForm[element]).
也许我可以以某种方式解决这个问题?
May be I can just fix this somehow?
我尝试更改belongsTo,但只替换了subForm"前缀.
I tried to change the belongsTo but that only replaced the "subForm" prefix .
实际上似乎缺少子表单上的belongsTo方法.
It actually seems like what is missing is a belongsTo method on the subForm.
同样,这完全是因为 ViewScript 装饰器.它适用于 FormElements 装饰器.
Again, this is all because of the ViewScript decorator. It works fine with the FormElements decorators.
更新 2: 只是为了澄清,我不介意这个名称更改,但是当我调用 form->populate 时它会导致我的字段没有填充.
UPDATE 2: Just to clarify, I wouldn't mind this name change, but it causes my fields to not populate when I call form->populate .
我认为我已经将问题缩小到这一点:当我在 setDefaults 中取回我的值时,它们的顺序如下:
I think that I've narrowed the problem to this: when I get my values back in setDefaults, they are ordered like this:
array(
\"formElements1-name\" => value1... \"subFormName\" => array(
\"parentFormName\" => array(
\"subFormName\" => subForm-values-array
)
)
...这里的主要问题是 "parentFormName" =>subFormNAme"..
它重复了什么?我已经在主要形式了.我猜这是因为我已经设置了 setElementsBelongTo(formName[subFormName])
,但如果我不这样做,那么我会让我的子表单值与表单完全分开,
...The main problem here is the "parentFormName" => "subFormNAme"..
what does it repeat itself? I'm already in the main form. I'm guessing this is caused because I've set the setElementsBelongTo(formName[subFormName])
, but if I wouldn't do that, then I would get my subform values completely separate from the form,
即值数组 = 数组(\"formName\" => 数组(表单值), \"subFormNAme\" => 数组(子窗体值)
i.e. values array = array( \"formName\" => array( formValues ), \"subFormNAme\" => array( subFormValues )
,而我希望它是
array(
formName => array(
subFormNAme => values-array
)
)...
是否有可能完成这项工作?
Is it even possible to make this work?
推荐答案
当前的解决方案是在子窗体上使用 PrepareElements 装饰器,只需进行一项更改 - 删除 PrepareElements 代码中的递归调用.此外,不需要setElementsBelongTo".
The current solution is to use the PrepareElements decorator on the subforms with one change - remove the recursive call in the PrepareElements code. Also, no "setElementsBelongTo" is required.
这似乎生成了正确的名称和 ID.
This seem to generate the correct names and ids.
这篇关于在嵌套子表单(Zend 表单)上使用 ViewScript 装饰器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!