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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.