views:

160

answers:

5

There have been a couple of discussions regarding the location of user input validation:

http://stackoverflow.com/questions/659950/should-validation-be-done-in-form-objects-or-the-model

http://stackoverflow.com/questions/134388/where-do-you-do-your-validation-model-controller-or-view

These discussions were quite old, so I wanted to ask the question again to see if anyone had any fresh input. If not, I apologise in advance.

If you come from the Validation in the Model camp - does Model mean OOP representation of data (i.e. Active Record/Data Mapper) as "Entity" (to borrow the DDD terminology) - in which case you would, I assume, want all Model classes to inherit common validation constraints. Or can these rules simply be part of a Service in the Model - i.e. a Validation service? For example, could you consider Zend_Form and it's validation classes part of the Model? The concept of a Domain Model does not appear to be limited to Entities, and so validation may not necessarily need to be confined to this Entities.

It seems that you would require a lot of potentially superfluous handing of values and responses back and forth between forms and "Entities" - and in some instances you may not persist the data recieved from user input, or recieve it from user input at all.

+3  A: 

When handling user input, you should definitely handle all logic-oriented validation outside the model.

The model doesn't care about your business logic. The model doesn't care if your start date is after your end date - all the model cares about is that the date is a valid entry for that particular field in the database. It checks the data, sees a properly formatted date and moves on to the next one, because the model's entire realm of responsibility is to ensure the smooth flow of data to and from a data source.

Classes like Zend_Form are nothing more than abstractions of your view.

Jarrod
That's an interesting interpretation of model. It is not shared by other environments/languages.
Stephan Eggermont
I'm awarding the bounty to Jarrod - I was waiting to see if there was a general consensus of opinion, but from this and other posts I see it is a bit of a contentious issue. Personally, I suspect that using a generic validation class would be more DRY than writing (or subclassing) validation rules into each model.
sunwukung
+1  A: 

I use Zend_Form as part of my models - models are creating the Zend_Form objects. I go this way because Zend_Form is not about the form rendering only; backed up with Zend_Validate and Zend_Filter it is a very powerful tool (my fav from the ZF stack). Matthew Weier O'Phinney wrote a nice post about using Zend_Forms in models: http://weierophinney.net/matthew/archives/200-Using-Zend_Form-in-Your-Models.html

robertbasic
+2  A: 

I much prefer to put validation in the model, personally. Security considerations of course are beyond the scope of what a model should be used for, but nothing says that a model is updated in exactly one place by exactly one form. By putting type validation and sanity checking outside the model, you have to validate every time you set anything on it, which leads to copy/pasted code that's difficult to update.

Sean Edwards
+1  A: 

Data validation should be on its own, called by the controller just before committing to the model.

redben
A: 

Haven't used PHP and haven't worked with Zend framework (have heard though), but I really like Jimmy`s blog post about validation from domain driven design perspective.

Arnis L.