Should you check the model? (Symfony-based question but related to general MVC)

This is a quick question related to Symfony, but may be a general MVC question.

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

Now I have made all my Symfony forms confirm that email_address is unique to this WebUser , however I am wondering if I should add this check for the model?

But it also made me wonder if you really should check each set() method in the model? It seems like a reasonable enough decision to make sure that there are no erroneous data in the database, but most (if not all) of the data should go through the controllers, which also check. So it seems to me that I am doing the same check twice, and that just seems pointless?

What do you think about this? I still tend to validate in the model, because it makes the most sense, because it dictates the business logic.

If you have to check in the model, how did you choose the appropriate set() error in Symfony, which is correctly handled by the form framework?

Thanks.

+6
php validation model-view-controller symfony1
source share
4 answers

I disagree with "Validation should be part of the domain logic, not interface logic."

Validation is a complex functional part of your application and should be contextual. i.e. you should know that the user is logged in, what credentials she has, the status of the request / form, etc. Models should instead be contextual (to work in any environment, not only an http request, but also cli, etc.), so they donโ€™t know about the user, status, and HTTP request. This is a strong requirement to test your model classes.

According to the alleged reason, the functional validation must belong to a form that knows the state of the application (i.e. the session). symfony helps a lot with the sfValidator * classes that really belong to the form component. The reason forms are tested with functional testing.

Data validation should be in the model instead (i.e. check if the value is an integer or a string, check if it is null, etc.). This is easily accomplished using validation rules within the Doctrine schema.

+6
source share

I canโ€™t talk specifically about Symfony, but I know that I deliberately avoid checking the form of the Zend Framework and instead test on my models (Zend Framework does not provide its own model component, so it has no actual opinion on this).

There is nothing wrong with validating the form, but I think you should also validate the model. Validation on the form can be useful for quick and easy verification of input, especially if the processing logic is complex - you will not waste time working with data that is clearly bad.

Reasons I think model validation is best:

  • The likelihood that the model will change the data after it passes through the form, and before it enters the database
  • Validation should be part of the domain logic, not the front-end logic (I understand that Symfony does not seem to agree.)
  • The check state moves with the model object instead of the form object.

If you are not completely sold, checking only in the model, a combination of two sounds as a good solution.

EDIT: In the end, it may make sense to just go with your card on this. If Symfony seems to be the most prone to validation in the controller and doesn't provide an easy way to validate in the model, just go with what they want to do for you (or the one in which the Symfony community relies). Struggling with a frame is never fun.

+3
source share

I cannot help with the symfony part, but most of the MVC-Framework do validation in the model, as this is the only place in the MVC environment where validation should be.
This applies to validating model attributes.

0
source share

I think you should use the MVC Validator Ket tool. When using other people, relying on modelstate is easier, as well as checking, and also based on formcollected not for a model that gives you a free way.

Regards, MArwan HAfez

0
source share

All Articles