Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real World MVC - Dealing with Forms

I'm still somewhat confused by how MVC is supposed to work.

Lets say I have a website selling widgets. I have a listing page, /widgets/list and a product page /widgets/product/123.

Both of these can use the widget controller and call the list and product methods - simple enough so far. Lets say I also have several other controllers for various things.

Now I add a newsletter signup box into my header - i.e. on every page of the site. How is this going to work? I get the idea that it should submit to /newsletter/signup

But what happens if there's an error (say you didn't fill in your email address correctly)? It should show whatever page you were on (e.g. /widgets/list) but the newsletter controller needs to run. The widget controller doesn't know about the newsletter controller so I can't put the code there... How is this supposed to work?

Edit: No AJAX please - I can understand that more easily. Consider this the fallback when javascript is disabled.

Edit 2: Any examples or tutorials covering this kind of thing would be much appreciated

Edit 3: Is it allowable for a view to call an action? For example the header might call Newsletter->index()

like image 621
Greg Avatar asked Sep 16 '09 13:09

Greg


2 Answers

I don't see why the error message for the newsletter box thats on every page, has to be rendered in the same page. If you have a page which posts to another action - completely unrelated to the current view (for example - search) then there is no reason for the error message to be shown in the original page. Would you show the success message on the same page? Where would that be handled?

Error messages for the newsletter form should be shown in a view dedicated to the newsletter. For example, see how its done in Stackoverflow - go into the search box and type nothing, simply hit enter. This is a kind of error since you did not specify what you want to search for. Stackoverflow will then take you to a different page that explains how search work.

Now why did it do that? The reason is simple - the user was on some page and chosen to engage in an activity that is not related to the current page, so there is no reason to keep them there.

like image 88
Guss Avatar answered Oct 18 '22 20:10

Guss


Add a field to the newsletter form, that stores the URL of the current page. When an error occurs during submission of the newsletter, retrieve the URL and redirect to that page. Providing that you put the error information in the correct place, it should be picked up by the newsletter form which, you say, is included in every page.

like image 22
belugabob Avatar answered Oct 18 '22 19:10

belugabob