• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Good practice

#1
[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?

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

#3
[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!

#4
[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.

#5
[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.

#6
[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.

#7
[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!

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

#9
[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.

#10
[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...893#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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.