Zend - 隐藏表单并显示消息

发布于 2024-12-08 04:39:35 字数 149 浏览 0 评论 0原文

我有一个带有几个字段的 zend 表单。

当表单验证时,我想隐藏表单并显示成功消息。

有很多方法可以做到这一点 - 重定向到另一个控制器操作、渲染另一个视图脚本、在视图助手/会话中标记等等。

这有什么最佳实践吗?

谢谢。

I have a zend form with a few fields.

When the form validated I would like to hide the form and show a success message.

There are so many ways to do it - redirect to another controller action, render another view script, flag in the view helper/session and more.

Is there any best practice for this?

Thanks.

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

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

发布评论

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

评论(3

昨迟人 2024-12-15 04:39:35

我认为人们经常使用其他两个答案中发布的解决方案,该解决方案检查表单是否已发布且有效,然后将消息传递到视图。

我认为这种方法还可以,但不是最佳的。它将消息与工作流程紧密联系在一起,并产生了代码重复的需要。这是没有必要的。 IMO 消息传递应从工作流程中剔除,并使用可重用组件进行处理。恰好,Zend Framework 为我们提供了当时必要的工具——flashMessenger。

因此,我开始做的是向 flashMessenger 发送一条消息,每当我需要一条消息时,它将在下一次往返后自动显示,独立于表单操作,并且不需要额外的标记和 if 语句。

有关 flashMessenger 的示例和资源:

FlashMessenger 与通过这些链接找到的附加视图助手配合使用效果最佳!

I think quite often people use the solution posted in two other answers which checks if the form has been posted and is valid and then passes a message to the view.

I think the approach is ok but not optimal. It ties messages closely into the workflow and creates the need for code repetition. This is not necessary. IMO messaging should be taken out of the workflow and handled with a reusable component. As it happens, Zend Framework provides us with then necessary tool, the flashMessenger.

So, what I started doing is sending a message to the flashMessenger, whenever I need one, it will be shown after the next roundtrip automatically, independent of the form action and takes away the need for additional markup and if statements.

Examples and resources concerning the flashMessenger:

FlashMessenger works best with the additional view helper found via these links!

寄风 2024-12-15 04:39:35

这是我的方式:

在控制器中,当表单提交成功时,我设置

$this->view->success_msg = "Success.";

并在视图文件中我只是问:

<?php
$success_msg = $this->success_msg;
if(isset($success_msg)){
    echo $success_msg;
}else{
    echo $form;
}
?>

This is my way:

In controller,when the form was submitted as successfull,I set

$this->view->success_msg = "Success.";

and in view file I just ask:

<?php
$success_msg = $this->success_msg;
if(isset($success_msg)){
    echo $success_msg;
}else{
    echo $form;
}
?>
伴梦长久 2024-12-15 04:39:35

我认为:

//into controller
$form = new Form_Foo();
if ($this->getRequest()->isPost() && $form->isValid($_POST)) {
    $this->view->message = "Post Successful!!!";
} else {
    $this->view->form = $form;
}

//into view
<?= $this->message ?>
<?= $this->form ?>

I think this:

//into controller
$form = new Form_Foo();
if ($this->getRequest()->isPost() && $form->isValid($_POST)) {
    $this->view->message = "Post Successful!!!";
} else {
    $this->view->form = $form;
}

and

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