Welcome Guest, Not a member yet? Register   Sign In
Data validation and processing. In controller or model? Getting error back to controller. Exceptions?
#1

[eluser]batfastad[/eluser]
I've read conflicting advice on whether I should be validating/processing data in the controller or the model. Both approaches seem to have benefits.
I also really really like using the form validation class. It's brilliant and saves making loads of if blocks. When I need to do a validation that's not supported by the class I just write a callback. So far this is the single biggest benefit to me of using a framework.

I quite like the idea of validating/processing in the model because it can really cut down the size of the controller. Leaving the controller purely there to define the flow of actions.

The downside I see to validating/processing in the model is returning error messages back to the controller.

I like to keep the return from functions, methods (and even models) consistent, so returning the same data type on true and false. So if I have a model that searches a database I like to either return an array of records, or an empty array.
For models that do INSERT, DELETE, UPDATE I would generally return TRUE if the query succeeded or FALSE if it failed.

If I am validating the data in my model I would also need to return an error message along with the TRUE/FALSE/data array of the query.

Does anyone do anything like this?
Code:
try {
$this->load->model('blog');
$this->blog->save_entry($this->input->post());
}
catch (Exception $e) {
$this->session->set('form_errors', $e->errors());
}
http://stackoverflow.com/questions/62177...exceptions

Or should I just return a multi-dimensional array back from the model with the the errors and the data array as sub-elements?

Or should I just keep my validation in the controller?

Cheers, B


Messages In This Thread
Data validation and processing. In controller or model? Getting error back to controller. Exceptions? - by El Forum - 10-29-2012, 01:23 PM



Theme © iAndrew 2016 - Forum software by © MyBB