我目前正在开发一个包含大量表单的大型应用程序.
I am currently working on a pretty large application which contains a lot of forms.
到目前为止,我一直在手工编写表单并编写自己的验证逻辑,但我决定是时候开始使用 Zend_Form 了,它是内置的验证例程.
Up to this moment, I have always been writing my forms by hand and writing my own validation logic, but I have decided it was about time I started using Zend_Form and it's built-in validation routines.
然而,我不断遇到越来越多关于(缺乏)灵活性的问题Zend_Form_Decorator
.向单个输入元素添加额外按钮等简单任务变得异常困难.
However, I keep stumbling upon more and more problems concerning the (lack of) flexibility caused Zend_Form_Decorator
. Simple tasks like adding an extra button to a single input-element become incredibly difficult tasks.
我现在已经到了认真考虑放弃 <代码>Zend_Form_Element + Zend_Form_Decorator
方法完全,但我不想失去优秀的验证选项.
I have now reached a point where I am seriously considering dropping the Zend_Form_Element
+ Zend_Form_Decorator
approach entirely, but I do not want to lose the excellent validation options.
基本上,我想要两全其美:
我正在考虑的一个可能的解决方案是在服务器端编写表单,就像在视图中一样.这将使我能够轻松地验证我自己的表单,但(在我看来相当大的)缺点是每个表单都应该定义两次,这感觉完全错误.
A possible solution I am considering is writing the forms both on the server side as in the views. This would allow me to easily validate my own forms, but the (in my eyes quite big) downside is that each form should be defined twice, which just feels plain wrong.
是否有任何指导方针可以做到这一点?有没有人遇到过同样的情况,如果有,你是如何解决这些问题的?
Are there any guidelines to do this? Have any of you experienced the same, and if so, how have you solved these issues?
我非常想听听您的观点.
I'd very much like to hear your points of view.
我也发现默认装饰器是一个巨大的痛苦.我理解为什么他们是这样的,但我认为不便因素"被严重低估了.
I too find the default decorators to be a massive pain. I understand why they are the way they are, but I think the 'inconvenience factor' has been grossly underestimated.
无论如何,我建议使用 用于您的表单的 ViewScripts.请注意,这些与 View 不同——相反,ViewScript 在您的 Form 类中被显式引用,充当各种子视图"并允许您控制每个元素的布局.过去很难找到如何使用 ViewScript 的示例,但我会尝试提供一些有用的东西.
Anyway, I would recommend using ViewScripts for your forms. Note that these are not the same as Views -- instead, ViewScripts are referenced explicitly in your Form class, act as a "sub-view" of sorts and allow you to control the layout of each and every element. Examples how to use ViewScripts have been somewhat hard to find in the past, but I'll try to take a stab at providing something useful.
首先,在你的表单类中覆盖 loadDefaultDecorators:
First, override loadDefaultDecorators in your form class:
public function loadDefaultDecorators() {
$this->setDecorators(
array(
array('ViewScript',
array('viewScript' => 'foo/bar.phtml')
)
)
);
}
这将引用位于 /views/scripts/foo 中的名为 bar.phtml 的 ViewScript.请注意上面ViewScript"和viewScript"中区分大小写的差异.
This will reference a ViewScript named bar.phtml located in /views/scripts/foo. Note the case-sensitive differences in "ViewScript" and "viewScript" above.
接下来,您必须调整应用于每个元素的装饰器以确保它显示但没有烦人的 dt/dd 包装器.例如:
Next, you'll have to tweak the decorators applied to each element to ensure it displays but without the annoying dt/dd wrappers. For instance:
$baz = new Zend_Form_Element_Text('bazInput');
$baz->setDecorators(array('ViewHelper','Errors'));
最后,您需要构建您的 ViewScript,例如:
Finally, you'll need to build your ViewScript, such as:
<form method="post" action="<?php echo $this-element->getAction() ?>">
<table>
<tr>
<td><label for="bazInput">Baz:</label></td>
<td><?php echo $this->element->bazInput ?></td>
</tr>
</table>
<input type="submit" value="Submit Form" />
</form>
显然这是一个非常简单的例子,但它说明了如何引用表单元素和表单动作.
Obviously this is a very simple example, but it illustrates how to reference the form elements and form action.
然后在您的视图中,像往常一样简单地引用和输出您的表单.通过这种方式,您可以更好地控制表单布局——包括轻松添加 Javascript.
Then in your View, simply reference and output your form as usual. This way you can have much finer control over your form layouts -- to include easily adding Javascript.
我相信这种方法可以解决您的两个需求:您可以使用纯 HTML 构建表单,并且仍然可以利用 Zend 表单验证机制.
I believe this approach resolves both your requirements: you can build forms in plain HTML and still take advantage of the Zend Form validation mechanism.
这篇关于Zend Framework 表单、装饰器和验证:我应该回到纯 HTML 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!