views:

337

answers:

4

Hi,

This is a quick question relating to Symfony, but could be a general MVC question.

I have a class in my model, for example, WebUser. This class has a property of email_address. This value must be unique to each WebUser.

Now, I've made it so all my Symfony forms validate that the email_address is unique for the given WebUser, however I'm wondering if I should add this validation to the model as well?

But this also got me thinking, should you actually validate every set() method in the model? It seems a wise enough decision to make sure no erroneous data ends up in the database, however most (if not all) data has to go through the controllers, which validate as well. So to me it seems I'm running the same validation twice and it just seems pointless?

What are your thoughts on this? I'm still leaning towards validation in the model as that makes the most sense as it dictates the business logic.

If you should validate in the model, how do you throw an appropriate set() error in Symfony that is handled correctly by the form framework?

Thanks.

A: 

I can't help out with the symfony part but most MVC-Frameworks do validation in the model, since this is the only place inside a MVC environment where validation should be.
This goes for the validation of the model's attributes of course.

Nils Riedemann
See, I thought this too, and it makes sense. However I've been doing reading and Symfony sees things a bit differently. They suggest some validation may not apply to some users, say admins, so the validation should be in the controllers for the given forms. And applying that idea to Symfony and how it's setup/works seems to make sense. Oh well, might just have to go down that route.
Stephen Melrose
+2  A: 

I can't speak specifically to Symfony, but I know that I purposely shun Zend Framework's form validation, and instead validate on my models (Zend Framework does not provide its own Model component, so it has no actual opinion on the matter).

There's nothing wrong with validating on the form, but I think you should also be validating on the model. Validating on the form might be useful for quick and easy input checking, especially if the processing logic is complex - you won't waste time working with data that's obviously bad.

Reasons I think model validation is best:

  • There's a chance a model will alter the data after it passes through the form and before it goes into the DB
  • Validation should be part of domain logic, not front-end logic (I realize Symfony seems to disagree).
  • Validation state travels with the model object, instead of the form object.

If you're not totally sold validating only in the model, a combination of the two sounds like a good solution.

EDIT: At the end of the day, it might make the most sense to just go with your framework on this. If Symfony seems most opinionated toward validation in the controller, and doesn't provide an easy path for validation in the model, just go with what they want you to do (or the direction in which the Symfony community leans). Fighting your framework is never fun.

Bryan M.
A: 

I Think You Should User MVC Validator Tool Ket Insted of using others rely on modelstate its easyer and also testable and its based on formcollected not to the model that give you free run way

Regards MArwan HAfez

MArwan
+4  A: 

I disagree with "Validation should be part of domain logic, not front-end logic".

Validation is a complex functional part of your application and must be context aware. ie. you have to know is the user is logged in, what kind of credentials she has, the status of the request/form and so on. Models instead must be context agnostic (to work in any environment not only http request but also cli etc.) so they don't know about the user, the state and the http request. This is a strong requirement for the testability of your model classes.

For the summentioned reason functional validation must belong to the form which knows the application state (ie. session). symfony helps a lot with the sfValidator* classes which belongs to the form component indeed. That's the reason why forms are tested with functional testing.

Data validation should be in the model instead (ie. check if the value is an integer or a string, check if it's null and so on). This is easily accomplished with the Doctrine in-schema validation rules.

gpilotino
+1 for making the distinction between functional validation vs. data validation. Although, I think most of the answers (including mine) only addressed data validation. I'm not sure I'd typically refer to what you described as 'validation', though. Usually just refer to it as controller or application logic. But that's just semantics.
Bryan M.