MVC 框架中如何使用表单?

发布于 2024-11-07 14:57:11 字数 154 浏览 0 评论 0原文

我已经做简单的 MVC 教程有一段时间了,我明白了这个概念。但我想知道,当表单显示在视图中时,表单是如何处理的?检查表单提交的代码必须存在于视图文件中,这并不真正适合,因为视图应该仅用于显示输出。

那么,当 MVC 框架的视图文件中有一个表单时,检查表单提交的代码应该在哪里呢?

i have been doing simple MVC tutorials for a while and i get the concept. But i am wondering, when a form is displayed in a view, how is the form processed? A code to check for form submission must be present in the view file, which doesn't really fit because view should just be for displaying output.

So when you have a form in a view file of an MVC framework, where should the code to check for form submission be?

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

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

发布评论

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

评论(4

苹果你个爱泡泡 2024-11-14 14:57:11

当然,这取决于具体的框架,但这是相当典型的:

  • 表单数据被发布到控制器(像所有请求一样)
  • 数据验证规则在模型中定义
  • 控制器通过模型运行数据进行验证
  • 如果成功验证,则 如果数据无效,控制器会执行其应执行的操作
  • ,控制器会将无效字段的错误消息推送到视图,
  • 视图仅显示错误消息

Of course this depends on the specific framework, but this is rather typical:

  • form data is posted to a controller (like all requests)
  • data validation rules are defined in the model
  • the controller runs the data through the model for validation
  • if successfully validated, the controller does whatever it's supposed to do
  • if data is invalid, the controller pushes error messages for invalid fields to the view
  • the view just displays the error messages
总攻大人 2024-11-14 14:57:11

表单提交可以在控制器中处理。检查这个

The form submission can be handle in controller. check this

战皆罪 2024-11-14 14:57:11

检查和验证表单提交的代码应该位于控制器或模型中,具体取决于从表单接收的数据类型以及您正在使用它执行的操作。这就是 MVC 的要点。视图文件应该只包含显示页面所需的最少量的逻辑。

The code to check for and validate the form submission should be in the controller or the model, depending upon the type of data received from the form and what you're doing with it. That's the point of MVC. View files should contain only the barest amount of logic necessary to display the page.

合约呢 2024-11-14 14:57:11

我认为最常见的方法是控制器,因为它是控制器处理所有输入数据(通过 $_POST、$_GET 等),然后最终决定调用哪些方法来处理该输入以及输出哪个视图。

I think the most common approach would be the controller, since it is the controller that handles all input data (via $_POST, $_GET etc) and then ultimately decides which methods to call to handle that input, and which view to output.

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