tags:

views:

43

answers:

2

I'm teaching myself MVC concepts in hopes of applying them to a non-OO/procedural development environment. I am pretty sure I understand simple View -> Request -> Controller -> Request -> Model -> Response -> Controller -> Response -> View flow.

What I am struggling with is understanding more complex scenarios. For instance, let's say I have a shopping cart form with a button for 'Calculate Shipping'. Normally a click on this button will follow the above flow. But what if there is missing data, like the zip code? Should the View verify this first and alert the user before making a 'Calculate Shipping' request? Or should the request be made and the Model returns a notification that critical data is missing? If the latter, does the Controller instruct the View to alert the user? What if I wanted to prompt the user for the missing zip code (perhaps in a popup input display) and then automatically request the 'Calculate Shipping' method again?

I suppose this gets into the question of how smart a View ought to be. It seems that MVC has evolved due to richer UI and automation (such as with data-binding) and this muddies the water from a purist MVC perspective. Any thoughts are greatly appreciated.

A: 

There are a fiew things you can do:

  1. Validate the zip code on the client side (in the browser) using Javascript or jQuery
  2. Send an Ajax request to the server to validate the zip code, and have the controller return a validation result to the browser, or
  3. Post the whole page, validate it, and return the same page with an error indication to the user, if the zip code is missing.

A reminder: you will still have to validate the data again on the server, once the page is POSTed. Never trust data coming from a web page, even if it has already been validated in the client.

Robert Harvey
While I was a little vague, I see where my example gave the impression that this was a 'web' app. In truth this is a traditional desktop GUI app. *However*, my interest in MVC is so that I can design the application to make it easy to plug-in alternative views (such as web pages.)Toward that end I appreciate the idea of using client-side logic (e.g. Javascript) to eliminate the overhead of a server request. I was worried that this would violate MVC. For instance, should the View really "know" that a zip code is necessary for shipping charges or should it be "told" that by the model?
Don Bakke
The view has to "know" about a zip code anyway, in order to display it. So validation client-side is just extending that knowledge. The pitfall is you have to do it twice; once for the web page, and once for the GUI (because you can't run Javascript in the GUI, and you can't run whatever language you're using in the GUI in a web page). But the view will, in both cases, *be displaying the same data model.* That's where the separation of concerns lies.
Robert Harvey
Good point. You are right: Javascript will not help my GUI and vice-versa. So, I would have to rewrite the client-side logic for both types of Views. However, if I rely on the Model then the core logic remains in one location and all of my Views can be relatively 'dumb' (i.e. require less rewriting to emulate the same functionality.)
Don Bakke
Yes, that is true. So it comes down to how intelligent you want the view to be. In theory the view can just be a template for the data, and nothing else. In practice, views are sometimes more clever than this, especially with web pages that require a little more finesse than a dumb view allows.
Robert Harvey
A: 

My own thought are that, for efficiency, as much as possible should be done in the view, as long as there are no data implications.

In other words, the view can check for missing zipcode or non-numeric zipcode (as long as you're certain the data model won't ever allow non-numerics).

But, the second you decide to check data (such as "Is the zipcode a valid one for the continental US?"), that should be referred to the data model itself.

I've seen situations where the view is created with a series of regular expressions for pre-validation (one per field) so as to not unnecessarily burden the other layers. This means that the data entered passes at least the first layer of sanity checks before being passed back for more complete validation.

And, as an aside, don't ever use the term "sanity check" in user documentation when referring to checking what the user has entered. You might think it's an innocent phrase but user don't always think so :-)

paxdiablo
Your distinction of when to rely on the Model for zipcode verification is interesting. Let's say that I *do* want to make a zipcode validation check against the Model. Is this the responsibility of my 'Calculate Shipping' method or a separate method altogether? If a separate method, does the Controller call it first and then route appropriately (i.e. back to the View if the zipcode is invalid or back to the 'Calculate Shipping' method if valid)?
Don Bakke
If it's your CalcShipping method that needs the zipcode, the validation would be best made part of that, I believe. I've seen generic validations on a view that would do this but I can envisage other methods called by your view which would _not_ require a zipcode. And, theoretically, the view would notify the controller to execute CalcShipping and the view would then update based on the results. But, as with all guidelines, plenty of people ignore them for efficiency :-)
paxdiablo