有没有办法从 Zend_Form 生成视图? (只读)
我想知道在处理 CRUD 时是否有一种简单的方法可以从表单对象生成视图。
我的意思是,当我们有这些选择时:查看|编辑 |删除 我希望我的“查看”选项像“编辑”选项一样,但没有表单元素,只有值。
这将最大限度地减少创建这些视图所花费的时间。
有人知道类似的事情吗?
I was wondering if is there an easy way to generate views from form objects when dealing with CRUDs.
I mean, when we have these options: VIEW | EDIT | DELETE
I want my VIEW option like EDIT option, but without form elements, just the values.
This will minimize so much the time spent to create these views.
Someone knowks something like that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在我的上一个项目中,我也遇到了这个困境。我的解决方案可能不是最优雅的,但它完成了工作。请注意,我使用表单视图脚本装饰器而不是完整的装饰器生成的元素。但我认为您可以调整此示例以使用装饰器。我所展示的是一个非常基本的示例,旨在为您提供一个总体概念。这就是我所做的:
所以,基本上,我将类常量之一传递给它的构造函数。根据该值,我确定表单需要哪些元素以及应如何呈现这些元素。
例如,对于创建,您可以有一个选择下拉表单字段,您可以在其中选择一个区域设置,对于删除,这将是一个隐藏字段(顺便说一句,在我的示例中未显示)。
希望这能给您一些想法。
PS:
在选定的视图脚本之一中,您可以简单地显示元素的值(也可以渲染隐藏元素),例如:
所以,我认为您最好使用表单的视图脚本装饰器,或者您可以滚动您自己的表单元素装饰器来呈现隐藏字段(如果需要)并简单地在某些 html 标记中显示它的值。
In my last project I had this dilemma too. My solution may not be the most elegant, but it did the job. Mind you, I use a Form viewscript decorator in stead of a full decorator generated elements. But you could adjust this example to use decorators I presume. What I'm showing is a very basic example, to give you a general idea. Here's what I did:
So, basically, I pass one of the class constants to it's constructor. And based on that value, I determine what elements are needed for the form, and how the elements should be rendered.
For instance, for create you could have a select dropdown form field where you would choose a Locale, where for delete this would be a hidden field (not shown in my example btw).
Hope this has given you some ideas.
PS:
In one of the selected viewscripts you could then simply show the value of an element (along with rendering the hidden element too), with something like:
So, I think you'ld be best of with using viewscript decorators for the form, or you could roll your own form element decorator that renders the hidden field (if neccesary) and simply shows it's value in some html tag.
来自 Nabble 的 Hector,
Hector from Nabble, show me this, which seems to be the best way:
我不确定我是否理解,但我认为对于视图选项,您可以从模型中获取数据。无需通过 Zend_Form 访问它们。
但是,如果您希望使表单只读,则可以向元素添加 readonly (setAttrib('readonly', 'readonly')) 属性。
I'm not sure if I understand, but I think that for the view option you can just fetch the data from your model. No need to access them through Zend_Form.
But if you want the make your form read-only, you can add readonly (setAttrib('readonly', 'readonly')) attribute to your elements.
对已接受的答案做了一些小的补充,以涵盖可能是特殊情况的常见元素:
Made a couple minor additions to the accepted answer to cover common elements that may be special cases:
接受的答案的唯一问题是您正在创建所有元素,然后忽略它们。
使用 fireeyedboy 的答案中的控制逻辑,您可以将所有元素切换到执行相同操作的
Zend_View_Helper_FormNote
。只是取决于优化是否重要。
The only problem with the accepted answer is that you're creating all the elements and then ignoring them.
Using the control logic from fireeyedboy's answer, you could instead switch all the elements to
Zend_View_Helper_FormNote
which does the same thing.Just depends on if the optimization matters.