Zend_Form ::什么时候应该在视图中而不是在控制器中创建表单? [英] Zend_Form:: When should be form created in view and not in controller?

查看:94
本文介绍了Zend_Form ::什么时候应该在视图中而不是在控制器中创建表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Zend_Form ::什么时候应该在视图中而不是在控制器中创建表单?

Zend_Form:: When should be form created in view and not in controller?

选项1-在控制器中创建并传递给视图的表单(通常使用)

控制器:

$form=new MyForm();
$this->view->form=$form;

视图:

echo $this->form;

选项2-直接在视图中创建表单(对我来说更好,因为它是视图的子部分)

视图:

$form=new MyForm();
echo $this->form;

谢谢

推荐答案

简而言之:更新中的视图.

您最终可能会

In short: newer in view.

You may eventually:

  • 创建用于复杂任务的视图助手(并在视图$this->getForm()中调用该助手),
  • 或使用Model::getForm()
  • service::getForm(),当您需要交叉操作表单时.
  • create view helper for complex tasks (and call the helper in view $this->getForm()),
  • or use Model::getForm()
  • or service::getForm() when you need cross-action forms.

因为在理想情况下,视图仅包含HTML,以将逻辑与表示(MVC)分开.

Because in the ideal case, views contain only HTML, to separate logic from presentation (MVC).

使用TDD时,您只为逻辑编写测试,而从不为视图脚本编写测试,这只是变量的衣服.

When using TDD, you write tests for logic, never for view scripts, which are only clothes for the variables.

显示表单不仅是表单本身,还包括检查表单是否已提交,检查验证错误,设置Flash Messenger变量等等.

Displaying the form, is not only the form itself, but also checking whether it was submitted or not, checking for validation errors, setting flash messenger variables and much more.

这些任务太复杂了,无法让它们查看脚本.

These are too complex tasks for putting them to view scripts.

作为区分逻辑和表示形式的一个很好的练习,我建议您看一下 PHPTAL 模板语言,是ZF中使用的原生PHP作为模板语言的不错替代.

As a good exercise on separating logic and presentation, I recommend you to take a look at PHPTAL template language, which is a nice alternative to native PHP as a template language used in ZF.

这篇关于Zend_Form ::什么时候应该在视图中而不是在控制器中创建表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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