Home » Php » php – Data and Form validation in MVC architecture

php – Data and Form validation in MVC architecture

Posted by: admin July 12, 2020 Leave a comment

Questions:

I develop most of my web applications using CodeIgniter, and have always took the approach of validating form data inside the controller, using the built in Form Validation class, before sending this data to the Model to be used, e.g insert the data into the database.

However, I keep hearing the “skinny controllers, fat models” line – and I’m wondering if these validation checks should be placed inside the model.

Three things strike me when I think of using this approach.

  1. How can different error messages be shown to the user, without returning seemingly ugly arrays or objects from these model functions? e.g A duplicate email on signing up for an account. Would the method responsible for adding a user in the model have to return an array or object to indicate if the insert was successful, and any error messages?

  2. By doing the validation checks in the model, checking variables supplied to the methods from the controller (not 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 like a CI library to mimic the Form Validation class, but for supplied variables, and not limited to the POST data?

  3. Following on from that concern…as the POST data would have to be validated for existence (isset($_POST['myvar'])) before being passed to the model, should the rest of the validation not just be placed in the controller as well?

Any suggestions, advice, opinions will be appreciated!

How to&Answers:

Your original issue comes from fact that CodeIgniter’s interpretation of MVC is quite appalling. This framework pretends that View is just a template, and Model is just an ORM ( which some say, should be classified as anti-pattern ). Which is completely wrong, and forces bot business and presentation logic inside the Controller.

But lets leave View aside.

Model in MVC is not a class or an object. Model is a layer, which contains all the business logic. It is actually comprised of instances from multitude of classes. The two most prevalent groups are Domain Objects [1] [2] (this is, what people people usually call “models”) and object responsible for information storage and retrieval – usually DataMappers. The model layer also contains standalone components (both your own and 3rd party) and higher level abstractions – services.

What you have as Validation class, might be considered to be a standalone component, which can either be used by Domain object to perform validation, or expect a Domain object to be passed in for validation .. depends on your implementation.

In your situation i would handle this at the service layer. Which would either provide the instance of View class with a valid domain object, or an object, which represents the error.

Some reading materials yu might be interest in:

Then again .. what the hell i know of it all ..