Where does the form processing logic belong to the MVC web application?

In a web application using the Model-View-Controller design pattern, the logic related to processing form representations seems to be somewhere between the model level and the controller level. This is especially true in the case of complex forms (i.e. when form processing goes far beyond simple CRUD operations).

What is the best way to conceptualize this? Are forms just some kind of glue between models and controllers? Or does formal logic belong directly to camp M or C?

EDIT: I understand the main flow of information in an MVC application (see chills42 answer for a summary). My question is, where does the form processing logic belong - in the controller, model, or somewhere else?

+5
source share
4 answers

I would say that this should probably be considered as two separate actions ...

  • presenting the form (V → C)
  • view processing (C → M)

Speaking in generics, I tend to think like every action, like a message between sections. A complete series of posts will be something like this ...

  • Display Form (C → V)
  • Submitted by (V → C)
  • The content of the process (C → M)
  • Processing completed (M → C)
  • Display Results (C → V)
+4
source

chills42, , .
(V- > C), , , , , , , , . ( ORM ) db, , .

+2

V → C, C → M, M → C , + + . , . , -, MVC .

, , , "webform" codeigniter. , validate(), process(), display() .

.

class User_controller
{

    function login()
    {
        $form = new LoginForm(); // this is the class you would create
        if ($form->validate())
        {
            $data = $this->user_model->getUserData( $form->userid );
            // form processing complete, use the main "user" model to fetch userdata for display,
            // or redirect user to another page, update your session, anything you like
        } else {
            $form->display();
        }
    }
}

. , :

  • , .

, -

$ this-> load-> form ("Input"); ......

However, this is just a suggestion that is useful for the codeigniter command.

+2
source

Form processing should be performed in the model, because it is an application layer that receives and processes data from controllers by type of views. The controller moves it, but as for the actual execution of the code, this should happen in your models.

+1
source

All Articles