play_ Posted May 21, 2011 Share Posted May 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237029-forms-in-mvc-patterns/ Share on other sites More sharing options...
ignace Posted May 21, 2011 Share Posted May 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237029-forms-in-mvc-patterns/#findComment-1218327 Share on other sites More sharing options...
play_ Posted May 21, 2011 Author Share Posted May 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237029-forms-in-mvc-patterns/#findComment-1218338 Share on other sites More sharing options...
ignace Posted May 21, 2011 Share Posted May 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237029-forms-in-mvc-patterns/#findComment-1218341 Share on other sites More sharing options...
trq Posted May 21, 2011 Share Posted May 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237029-forms-in-mvc-patterns/#findComment-1218342 Share on other sites More sharing options...
spiderwell Posted May 21, 2011 Share Posted May 21, 2011 i've been thrown in at the deep end with MVC on my new job, i love learning curves like this!! Quote Link to comment https://forums.phpfreaks.com/topic/237029-forms-in-mvc-patterns/#findComment-1218349 Share on other sites More sharing options...
play_ Posted May 21, 2011 Author Share Posted May 21, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/237029-forms-in-mvc-patterns/#findComment-1218365 Share on other sites More sharing options...
sharal Posted May 21, 2011 Share Posted May 21, 2011 Here's a good place to start, and yes - implementing the pattern yourself does give you a better understanding of how it works. You could for instance start here: http://net.tutsplus.com/tutorials/php/create-your-first-tiny-mvc-boilerplate-with-php/ Quote Link to comment https://forums.phpfreaks.com/topic/237029-forms-in-mvc-patterns/#findComment-1218378 Share on other sites More sharing options...
ignace Posted May 21, 2011 Share Posted May 21, 2011 data validation rules are defined in the model -- link Data validation is done in the model not checking whether the form was submitted. Quote Link to comment https://forums.phpfreaks.com/topic/237029-forms-in-mvc-patterns/#findComment-1218379 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.