Jump to content

Forms in MVC patterns


play_

Recommended Posts

Creating my own little MVC framework to understand it better.

 

Question -

 

When a view outputs a page with a form in it, how is the form handled? ie, what is the action of the form?

Where does the IF statement that checks if the submit button was pressed, go? I was told inside a method in the Model...but I don't understand how a method can check if a form has been submitted or not.

 

Any help greatly appreciated.

Link to comment
Share on other sites

The IF statement that checks whether the user pressed submit DOES NOT go inside the Model, ever! MVC is respectively 3 layers: Business, Presentation and Application. Only the Application layer can handle requests. GET and POST are requests so naturally only the Application layer can handle them, delegating work to the other layers.

 

Handle the POST in the Controller, delegate the information to the Model.

 

If your Model handles Application logic then it becomes less re-usable. To give you a better example: In Java you can develop applications for the desktop but also create Java server pages. You could re-use the same Model here serving both desktop and web.

Link to comment
Share on other sites

Ok but -how- would i handle the POST?

 

Do i make the form action go to the controller? and if so, what happens then? the IF statement will be inside a method. I need a way to call that method.

Link to comment
Share on other sites

class UsersController extends Zend_Controller_Action
{
  public function registerAction()
  {
    if ($this->_request->isPost()) {
      $tbl = new UsersTable();
      $row = $tbl->createRow($this->_request->getPost());
      $row->save();
      $this->_redirect('/users/login');
    }
  }
}

 

<form action="/users/register" method="post"></form>

 

The game doesn't change because they changed the rules.

Link to comment
Share on other sites

Have you used any of the MVC frameworks that are around play_ ? It seems to me that you might not understand the concepts at that stage.

 

No, not yet. I read that "everyone should build their own MVC framework" to understand it best. So that's where I started.

 

The comments on the 3rd post here (amongst others somehwere) indicate that the Model should be handling the form.

 

http://stackoverflow.com/questions/6015040/how-are-forms-used-in-an-mvc-framework

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.