Define Model Verification

Several discussions were held regarding the location of user input validation:

Should checks be performed on form objects or in the model?

Where do you check? model, controller or view

These discussions were quite old, so I wanted to ask a question again to find out if anyone has any fresh input. If not, I apologize in advance.

If you came from the "Validation in Model" camp - does the model mean representing OOP data (for example, Active Record / Data Mapper) as "Entity" (to borrow DDD terminology) - in this case, I suppose, you want all classes models inherited general validation constraints. Or can these rules just be part of the Service in the Model - that is, the verification service? For example, could you consider Zend_Form and its verification classes for the Model part? The concept of a Domain Model is not limited to entities, so validation does not have to be limited to these objects.

It seems that you will need a lot of potentially redundant transfer of values ​​and responses between forms and "Entities", and in some cases you will not be able to save data received from user input, or receive it from user input at all.

+4
source share
6 answers

When handling user input, you must be sure to handle all logically oriented checks outside the model.

The model does not care about your business logic. The model does not care about whether your start date will be after the end date - the whole model takes care that the date is a valid entry for this particular field in the database. He checks the data, sees a correctly formatted date and proceeds to the next, because the model of the entire sphere of responsibility is designed to ensure a smooth flow of data to and from the data source.

Classes such as Zend_Form are nothing more than abstractions of your representation.

-3
source

I prefer to put validation in the model, personally. Of course, security considerations go beyond what the model should be used for, but nothing suggests that the model is updated in exactly one place using one form. By checking the type and checking the health outside the model, you should check every time you install something on it, which leads to copied / pasted code that is difficult to update.

+2
source

I use Zend_Form as part of my models - models create Zend_Form objects. I go this way because Zend_Form is not only visualization of the form; using Zend_Validate and Zend_Filter 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

+1
source

Validation of data should be on its own, caused by the controller immediately before moving on to the model.

+1
source

I did not use PHP and did not work with the Zend card (I heard, though), but I really like Jimmy`s blog post about validation in terms of domain design.

0
source

I examined many different approaches to data validation and decided that the best way to validate is to validate before creating an entity, since validation is something that can be very context sensitive and the entity itself should not perform validation because the object should always be in correct condition. Therefore, perhaps the best approach is to use separate validation classes to validate the data before passing it to the entity constructor.

0
source

Source: https://habr.com/ru/post/1312095/


All Articles