Validating data and forms in MVC architecture

I develop most of my web applications using CodeIgniter and have always taken an approach to validating form data inside the controller using the built-in form validation class before sending this data to the model that will be used, for example, to insert data into the database.

However, I continue to hear the line "skinny controllers, bold models" - and I wonder if these checks should be placed inside the model.

Three things hit me when I think about using this approach.

  • How can various error messages be displayed to the user without returning the seemingly ugly arrays or objects from these model functions? for example, duplicate email when registering with an account. Will the method responsible for adding the user to the model need to return an array or object to indicate whether the insert was successful, and any error messages?

  • By performing validation checks on the model, checking the variables provided to the methods from the controller (not the POST data), I will lose the use of the form validation class, a class that I find very useful in my projects. Would you suggest that I write a class or library that can be used as a CI library to simulate a form validation class, but for supplied variables and not limited to POST data?

  • Following this concern ... since the POST data should be checked for existence ( isset($_POST['myvar']) ) before passing the model if the rest of the check is not placed in the controller as well?

Any suggestions, tips, opinions would be appreciated!

+8
php architecture model-view-controller codeigniter
source share
1 answer

The original problem comes from the fact that the interpretation of MVI in CodeIgniter is quite horrific. This structure pretends that View is just a template, and Model is just ORM ( which, according to some words, should be classified as an anti-template ). Which is completely wrong, and forces the logic of the business logic and presentation into the controller.

But let's leave the View aside.

A model in MVC is not a class or object. A model is a layer that contains all the business logic. It actually consists of instances of many classes. The two most common groups are Domain Objects [ 1 ] [ 2 ] (this is what people usually call "models") and the object responsible for storing and retrieving information is usually DataMappers . The model layer also contains autonomous components (both your own and third-party ones) and higher-level abstractions - services.

What you have as a Validation class can be considered a stand-alone component that can be used by a Domain object to perform validation or expect that the Domain object that will be passed in for verification depends on your implementation.

In your situation, I would deal with this at the service level. Which would either provide an instance of the View class with a valid domain object, or with an object representing an error.

Some reading materials may be of interest:

And again .. what the hell do I know about all this.

+4
source share

All Articles