CodeIgniter Forums
Good practice - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Good practice (/showthread.php?tid=43379)

Pages: 1 2 3


Good practice - El Forum - 07-09-2011

[eluser]shadow player[/eluser]
Which one of the following procedures seems better or more correct to you?

1.

- Controller receives input
- Controller uses validation class to validate data
- Controller either shows error view or passes data to model
- Model stores data into database returns positive to controller
- Controller shows success view

2.

- Controller receives input
- Controller forwards input to model
- Model validates data through validation class
- Either returns errors or positive to controller
- Controller shows either errors or success view

What do you think?


Good practice - El Forum - 07-09-2011

[eluser]mi6crazyheart[/eluser]
If there is no special need of doing all validation at MODEL, then i'm with OPTION-1...


Good practice - El Forum - 07-09-2011

[eluser]shadow player[/eluser]
If models are data-handlers wouldn't they also be responsible for validating the data they may process?

Quote:If there is no special need of doing all validation at MODEL, then i’m with OPTION-1…

Could you think of an example scenario?

Thanks for the reply btw!


Good practice - El Forum - 07-09-2011

[eluser]Mirge[/eluser]
It boils down to personal preference, IMO.

I prefer to leave all validation in the controller, and let the model focus strictly on accessing/updating the database.

You're bound to get many different answers.


Good practice - El Forum - 07-09-2011

[eluser]jmadsen[/eluser]
I would say validation is business rules, and should be left in the controller.

The model should only be concerned with database integrity.


Good practice - El Forum - 07-09-2011

[eluser]adityamenon[/eluser]
All controllers are supposed to be "butlers", carrying data to and from the kitchens. They do nothing but co-ordinate. It makes more sense to go for Option - 2. Model is where the "business logic" is supposed to be, and input validation is business logic IMO.


Good practice - El Forum - 07-10-2011

[eluser]shadow player[/eluser]
So I asked the PHP experts over at StackOverflow and their answer was that they all validate data in the model and that we should keep models fat and controllers thin.

Thanks for the replies guys, you're awesome!


Good practice - El Forum - 07-10-2011

[eluser]jmadsen[/eluser]
can you post a link to that thread? I'd be interesting in reading their reasons for that


Good practice - El Forum - 07-11-2011

[eluser]moodh[/eluser]
What happens when you want to access the same model without validation?
Lets say you want to validate user input but not admin input, through the same model?
Would you then create two different model methods that does the same thing or one with if-blocks to handle it?

That makes no sense at all, thats why you go with validation in the controller.


Good practice - El Forum - 07-11-2011

[eluser]shadow player[/eluser]
Quote:can you post a link to that thread? I’d be interesting in reading their reasons for that

http://chat.stackoverflow.com/transcript/message/985893#985893

Quote:Lets say you want to validate user input but not admin input, through the same model?

Admin input has to be validated too. Stuff admins can input and users can't (i.e. HTML in a post) are commonly set on a permission level.
If an admin accesses a common routine in a model (i.e. creating an user) that would 'normally' be accessed on user registration - he still has to enter a valid email address for the user.