Zend Framework / Form Element 呈现为文本框而不是下拉框
我在 config.ini 文件中有以下内容: (Zend_Form_Element)
site_status.name = "site_status"
site_status.type = "select"
site_status.label = "Status"
site_status.options.multiOptions.active.key = "Active"
site_status.options.multiOptions.active.value = "Active"
site_status.options.multiOptions.active.key = "Inactive"
site_status.options.multiOptions.active.value = "Inactive"
如您所见,这应该是一个下拉(选择)框,但它被呈现为标准文本框。我做错了什么?
-->编辑
我不是将元素绑定到表单,而是尝试将它们绑定到数据库:在我的代码中,它看起来像这样:
[{tablename}] // the table name would represent a section in the ini
{column}.name = "{column_name/form_field_id}";
{column}.type = "{form_element_type}"
{column}.label = "{form_element_label}"
...
从那里我将拉入表单将代表数据的数据库表(根据需要一张或多张表)。就这种方法的理由而言,我想定义(通过 ini 或其他存储方法)一个配置文件,它是属于特定表单的字段/元素的列表(非程序员类型可以轻松编辑),“通用”表单类将读取、提取元素信息并动态创建表单。
然而,我确实意识到这提出了另一个我尚未弄清楚的问题,那就是如何对选择元素使用表查找(无需将查找的数据库检索编码到表单中,以便非用户可以轻松地只是无需任何编程,纯粹的配置即可定义它,但这是一个完全不同的主题,不属于我的问题的一部分(无论如何,我认为我对这部分问题有可行的想法/解决方案)——额外的配置条目和通用例程。 。
我希望这能澄清我的问题 思考过程以及我按照上面示例中的方式这样做的原因。
I have the following in a config.ini file: (Zend_Form_Element)
site_status.name = "site_status"
site_status.type = "select"
site_status.label = "Status"
site_status.options.multiOptions.active.key = "Active"
site_status.options.multiOptions.active.value = "Active"
site_status.options.multiOptions.active.key = "Inactive"
site_status.options.multiOptions.active.value = "Inactive"
As you can see this is supposed to be a dropdown (select) box, however it is being rendered as a standard text box. What am I doing wrong?
--> Edit
Rather than tying the elements to a form, I am trying to tie them to a database: In my code it would look something like this:
[{tablename}] // the table name would represent a section in the ini
{column}.name = "{column_name/form_field_id}";
{column}.type = "{form_element_type}"
{column}.label = "{form_element_label}"
...
From there I would pull in the database table(s) that the form would represent data for (one or more tables as necessary). As far as the reasoning for this approach is that (down the road), I want to define (either by ini or some other storage method), a configuration file that would be a list of fields/elements that belong to a specific form (that a non-programmer type could easily edit), that the 'generic' form class would read, pull in the element info, and create the form on the fly.
I do realize however this poses another problem which I haven't yet figured out, and that is how to use table lookups for select elements (without coding the database retrieval of the lookup into the form, so that a non-user could easily just define it without any programming, purely configuration, but that is a whole other topic not part of my question here. (and I think I have viable ideas/solutions to that part of the problem anyhow) -- extra config entries and a generic routine pretty much.
I hope that clarifies my thought process and reason why I am doing it the way I am in the example above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有尝试过使用
Zend_Config
来构造Zend_Form
的实例。但看一下代码就会发现
Zend_Form::addElement()
并不直接将Zend_Config
实例作为参数。相反,您似乎需要将Zend_Config
实例传递给表单构造函数。似乎配置格式需要更深一些,以便将配置键映射到setXXX()
调用。在
path/to/config/myForm.ini
中:然后实例化:
未测试,但查看此示例:
将 Zend_Form 与 Zend_Config 一起使用 - Andrew Vayanis
感觉应该像上面这样。
更新
鉴于@Aaron 的评论/反馈,还有两种方法。
我们可以扩展
Zend_Form
,实现一个名为addElementByConfig
的方法,其中我们将传递描述元素本身的浅层Zend_Config
实例。事实上,我们甚至可以采用递归方法重写addElement()
:如果第一个参数是Zend_Config
的实例,则调用addElement()< /code> 使用组件数据。
如果原子性和可重用性是我们在使用
Zend_Config
描述元素时寻求的主要好处,那么也许我们只需创建一个扩展Zend_Form_Element
的自定义元素即可。然后我们可以以任何我们希望的形式使用这些元素。I have not yet played with using a
Zend_Config
to construct an instance ofZend_Form
.But a look at the code suggests that
Zend_Form::addElement()
doesn't directly take aZend_Config
instance as a param. Rather, it looks like you need pass yourZend_Config
instance to the form constructor. It also seems that the config format needs to be a little deeper in order to map config keys tosetXXX()
calls.In
path/to/config/myForm.ini
:Then instantiating:
Not tested, but looking at this example:
Using Zend_Form with Zend_Config - Andrew Vayanis
it feels like it should go something like the above.
Update
In view of the comments/feedback from @Aaron, two more approaches.
We could extend
Zend_Form
, implementing a method called something likeaddElementByConfig
in which we would pass the shallowZend_Config
instance that describes the element itself. In fact, we could even just overrideaddElement()
, taking a recursive approach: if the first param is an instance ofZend_Config
, then calladdElement()
using the component data.If the atomicity and re-usability are the primary benefits we seek in using
Zend_Config
to describe an element, then perhaps we just make a custom element extendingZend_Form_Element
. Then we could use these elements in any forms we wish.