开发者

How are forms used in an MVC framework?

开发者 https://www.devze.com 2023-03-06 16:34 出处:网络
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

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?


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


The form submission can be handle in controller. check this


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消