Zend 不同的视图脚本?

发布于 2024-08-02 05:16:27 字数 147 浏览 8 评论 0原文

我有一个控制器,它将输入从表单传递到模型类中以执行验证。 如果验证成功,我希望允许流程继续并呈现与控制器关联的默认视图。

我的问题是,如果验证不成功,那么我希望模型传回验证错误消息并将其显示在单独的视图中。如何在替代视图上设置错误消息?

提前致谢。

I have a controller that passes input from a form into a model class to perform validation.
If the validation is successful I want to allow the flow to continue and render the default view associated with the controller.

My issue is that if validation is not successful then I want the model to pass back validation error messages and display them in a separate view. How can I set the error messages on the alternative view?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故人如初 2024-08-09 05:16:27

好吧,从控制器中,您可以将它们重定向到另一个控制器中的另一个操作:

$this->_forward($newactionname,
                        $newcontrollername,
                        $newmodulename,
                        Array($parameters_to_pass);
    }

或者您只是渲染一个不同的视图文件:

$this->render('index_alternative');

Well, from the controller you can redirect them to another action in another controller:

$this->_forward($newactionname,
                        $newcontrollername,
                        $newmodulename,
                        Array($parameters_to_pass);
    }

or you just just render a different view file:

$this->render('index_alternative');
空宴 2024-08-09 05:16:27

如果要重定向到同一控制器中的操作,请不要使用 _forward(),只需使用 $this->fooAction() 直接调用操作,而不是 this->_forward('foo'...

原因是由于控制器被构造而可能发生的性能和错误,当您调用 _forward 时,不仅预调度会再次运行(这是预期的),而且如果您扩展了控制器,也会再次调用 init() 和构造函数。从其他控制器,所有这些控制器也会被调用,包括它们的 init()。如果你的 init() 中有代码,它会运行两次,如果你正在写入数据库,它会写入该行两次!整个事情并直接调用操作并使用 $this->render() 代替,

如果你分析你的代码,你可以很容易地看到这个问题,

Don't use _forward() if you are redirecting to actions in the same controller, just call the action directly using $this->fooAction(), rather than this->_forward('foo'...

The reason is performance and errors that can occur due to the controller being constructed wtice. When you call _forward not only the predispatch run again (which is something to be expected) but init() and the constructor also gets called again. If you have your controller extend from other controllers, than all those controllers will be called too, including their init(). If you have code in your init() is it will run twice, and if you are writing to a database it will write the line twice! Avoid the whole thing and call the action directly and use $this->render() instead.

You can easily see this issue if you profile your code,

长不大的小祸害 2024-08-09 05:16:27

为什么要在不同的视图中显示错误消息?为什么不将条件构建到视图中?类似如果表单有错误则回显消息,否则回显表单。

您可以使用 $this->_forward 转发到另一个操作及其各自的视图。你可以传递任何你想要的东西。只需传递表单对象,它包含所有错误消息。或者,您可以从表单对象中检索某些错误消息或全部错误消息,并将它们传递给视图或操作。

Why do you want to display the error messages in a different view? Why not build conditionals into the view? Something like if form has errors then echo messages else echo form.

You could use $this->_forward to forward to another action with its respective view. You can pass along whatever you wish. Just pass the form object along, it contains all the error messages. Or you can retrieve certain error messages or all of them from the form object and pass them to a view or an action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文