How are forms used within MVC?

I went in for some simple MVC tutorials for a while and I got the concept. But I am wondering, when the form is displayed in the view, how is the form processed? The code for checking the form submission must be present in the view file, which is not suitable, because the view should simply be displayed on the output.

So, when you have a form in an MVC structure view file, where should the code to submit the form be checked?

+3
source share
4 answers

The code to validate and verify the presentation of the form must be in the controller or model, depending on the type of data received from the form and what you do with it. This is the point of MVC. Viewed files should contain only the largest logic needed to display the page.

+1
source

Of course, this depends on the specific structure, but this is pretty typical:

  • form data is sent to the controller (like all requests)
  • data validation rules are defined in the model
  • the controller runs the data through the model to verify
  • if it is successfully verified, the controller does everything it should do
  • If the data is invalid, the controller displays error messages for invalid fields in the view
  • only error messages are displayed in the view
+4
source
+3

I think that the controller would be the most common approach, since it is the controller that processes all the input data (through $ _POST, $ _GET, etc.), and then ultimately decides which methods to call to process this input, and what kind output.

+1
source

All Articles