Zend Form SetAction 使用命名路由
我有一个表格,我正在尝试为其设置操作。我想使用我在引导程序中创建的路由在表单文件(扩展 Zend_Form)中而不是在控制器或视图中声明操作。 通常,当我想使用路线时,我会
$this->url(array(), 'route-name');
在视图或
$this->_helper->url(array(), 'route-name');
控制器中执行类似的操作。
如何从 Zend_Form 中调用路线?
编辑: 我已经放弃尝试将路线加载到 zend_form 中。也许在未来的版本中可能有一个功能可以轻松做到这一点?
我为我的表单创建了一个 viewScript 并在其中设置了路线: 在表单 init 函数中:
$this->setDecorators(array(
'PrepareElements',
array(
'ViewScript', array(
'viewScript' => 'forms/formView.phtml'
))));
和在视图文件中:
<form method="post" action="<?php echo $this->url(array(), 'route-name'); ?>" enctype="application/x-www-form-urlencoded">
<?php
foreach ($this->element->getElements() as $element)
{
echo $element;
}
?>
</form>
I have a form that I am trying to set the action for. I want to declare the action inside my form file (which extends Zend_Form) instead of in a controller or view, using a route I have created in my bootstrap.
Usually when I want to use a route I do something like
$this->url(array(), 'route-name');
in the view, or
$this->_helper->url(array(), 'route-name');
in the controller.
How do I call a route from within Zend_Form?
edit:
I have given up trying to load a route into zend_form. Perhaps in a future release there may be a function to easily do this?
I have created a viewScript for my form and set the route in that:
In the form init function:
$this->setDecorators(array(
'PrepareElements',
array(
'ViewScript', array(
'viewScript' => 'forms/formView.phtml'
))));
and in the view file:
<form method="post" action="<?php echo $this->url(array(), 'route-name'); ?>" enctype="application/x-www-form-urlencoded">
<?php
foreach ($this->element->getElements() as $element)
{
echo $element;
}
?>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
方法 1:获取路由器
方法 2:获取 View 对象的实例并直接调用 url-view-helper
我更喜欢方法 1。它更详细,但表单中的依赖项少了一个。
Method 1: Get the router
Method 2: Get an instance of the View object and call the url-view-helper directly
I prefer Method 1. It is more verbose but you have one dependency less in your form.
如果在我的控制器操作中:
我将使用视图助手 url 在我的视图脚本 (xxx.phtml) 中生成表单操作 url:
if in my controller action:
I will use view helper url to generate the form action url in my view script ( xxx.phtml):
现在,您可以通过
Zend_Form
类上的getView()
方法访问Zend_View
对象:这可能对 ZF 1.8+ 有帮助
Nowadays you can access the
Zend_View
object viagetView()
method onZend_Form
classes:Might this help on ZF 1.8+
我不知道它是什么时候添加的,但有一个更简单的解决方案。
您可以使用 getView() 检索表单的视图对象,该对象可以访问已注册的路由。
I do not know when it was added, but there is an even simpler solution.
You can retrieve the form's view object with getView(), which has access to the registered routes.