Personally, I like having validation inside of or as close as possible to the
object that stores the data (the Model). When you're talking about a high level
MVC where the View is the client making requests (web browser, WAP device,
Flash App, etc.), if you want client-side validation then obviously you're
going to have validation code in the View modules as well - there isn't
anything wrong with this. I don't typically have validation in Controller code -
the job of the Controller is to update the data in the model and update the
view if necessary - the model should throw exceptions when the controller
attempts to update it with invalid data and any validation in the view should
be a preliminary attempt to prevent those exceptions from happening.  I find
this leads to more manageable code. One thing to note is that if you don't have
time to implement robust exception handling, you might be better off validating
at the controller level - purely because exception handling then only need be
implemented in two tiers rather than 3.  Also consider that you'll make life
easier for yourself if you implement a pattern-based exception handler as part
of the controller - I recommend looking at something like the "Service to
Worker" pattern or even a simple decorator pattern based front controller...
though decorator patterns are really a bit overly complex for this sort of
functionality. Of course, it does really depend on the application as well.

~Simon

>
> Hello
>
> I'm been trying to use CFC's in a more OO way using a sort of MVC
> methodology.  I'm getting my head round most of the concepts but I'm
> kinda stumped at one question in my head,  Could anybody provide any
> pointers?   When considering user input data from the View layer, should
> data validation happen in the Controller or in the Model layer?
>
> My own initial thinking was that the Model should just ensure basic
> validation and throw an error if it encounters a value that is incorrect
> (i.e. as long as it's numerical, then that's okay, if it's a string,
> then throw an error, not interested in whether it's a number within a
> certain range).  The controller has the main data validation and calls
> the object only after all data has been validated.
>
> I'm now seeing a lot of problems in my head with using this technique
> and was hoping some of the OO crowd could give me some pointers in this
> note.
>
> TIA
>
> --
> dc
>
>
[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings] [Donations and Support]

Reply via email to