Zend framework: what is the right place to validate user input?

php, validation, zend-framework

Solution

I would say put the validation in your model. You can then keep your validation rules in a central location. How should your controller know the exact length of a valid user name? That is model territory. Your controller can ask the model if a user name length is correct or not sure, but the rule itself needs to be in your model. In my controller I would do something like this:

$model = new Model; $model->loadFromArray(something to get post); if (!$model->isValid()) { forward back to form } $model->save();

Problem

I want to add a user in users table via link like '/index/adduser/id/7' . Question Should I validate user input inside 'adduserAction' function inside controller or somewhere inside model file? I've put files containing database related functions inside 'models' directory. Suppose a user is added to a table via 'id'. This id is sent via 'get'. And finally its added to table by 'AddUser' function (inside model file). Then I should validate this 'id' inside 'adduserAction' or 'AddUser'.? Scalability-wise, would it be better to do it inside 'AddUser'?

Original source