MVC [Zend Framework]: where to apply filtering and validation

I have (I hope) a very simple question for experts in the MVC framework:

Where to apply input filtering and validation? Controller or model?

I read a lot of guides and guides on filtering user input, but did not notice a lot of discussion where it should be applied. Using forms is simple, in fact almost everything is done for you by Zend_Form through Zend_Filter and Zend_Validate.

But when I need to filter out one value from user input and pass it to the model, what is the best way to clean it before passing it to the model or the model itself?

Suppose I create a model that other people will use, and it does important work on the file system. I am 100% sure that other people will prepare the parameters correctly before passing them to the model? I am not, so it would be best to clear the parameters in the model itself.

But these are just my thoughts, and, as I said, I would like to hear yours, directly from the masters of the profession;)

A good day.

+3
source share
3 answers

IMHO it depends on whether you know in advance what validation you will need to do.

If this is something that can be expressed as a regular expression, leave it in the controller, otherwise I think the model should be its place.

<strong> Examples.

: , .

, : , ; , .

, $x , - : model.

+2

. , , ( , , ) , .

+1

Usually you do this in the controller. The model must deal with legitimate, useful data.

0
source

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


All Articles