Zend Framework表单,装饰器和验证:我应该回到纯HTML吗? [英] Zend Framework forms, decorators and validation: should I go back to plain HTML?

查看:66
本文介绍了Zend Framework表单,装饰器和验证:我应该回到纯HTML吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理一个包含很多表格的大型应用程序.

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 .请注意,它们与Views不同-而是在Form类中显式引用ViewScripts,它们充当各种子视图",并允许您控制每个元素的布局.过去很难找到使用ViewScripts的示例,但我将尽力提供一些有用的东西.

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 Form验证机制.

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吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆