問題描述
我目前正在開發一個包含大量表單的大型應用程序.
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.
基本上,我想要兩全其美:
- 以最終用戶看到的方式編寫表單:使用純 HTML
- 輕松地向表單字段添加服務器端驗證,而不會破壞太多 ZF 標準行為
我正在考慮的一個可能的解決方案是在服務器端編寫表單,就像在視圖中一樣.這將使我能夠輕松地驗證我自己的表單,但(在我看來相當大的)缺點是每個表單都應該定義兩次,這感覺完全錯誤.
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模板網!