CodeIgniter Forums
Data validation and processing. In controller or model? Getting error back to controller. Exceptions? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Data validation and processing. In controller or model? Getting error back to controller. Exceptions? (/showthread.php?tid=55497)



Data validation and processing. In controller or model? Getting error back to controller. Exceptions? - El Forum - 10-29-2012

[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/6217786/codeigniter-and-throwing-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


Data validation and processing. In controller or model? Getting error back to controller. Exceptions? - El Forum - 10-29-2012

[eluser]Aken[/eluser]
I always do form validation in the controller. That's what the controller is for, in my opinion - handling input data and delegating it appropriately. I also have never run into a situation where I needed to reuse the exact same form validation in more than one controller.

That said, I also do basic validation/sanitizing in my models, that prevent improperly formatted data from affecting code in my model. Depending on the severity of the error (and the type of project I'm doing, sometimes), I'll throw an Exception (severe error) or save the error to a property and return false. At which point I can call Model->getError() or what have you if I need. It's really up to you; there's no defined best practice there.


Data validation and processing. In controller or model? Getting error back to controller. Exceptions? - El Forum - 10-29-2012

[eluser]batfastad[/eluser]
Yeah to me I think it makes more sense to do form validation in the controller. Depending on the result of that validation you might not want to load/access the model.
E.g. if you have a form that POSTs to itself then you'll want to check if $this->input->post() is true or false to either process the form input or display your HTML form.

Also I can't foresee a situation where a query (in the project that prompted this question at least) will need to be performed by forms from different pages. That's the benefit I can see of doing form validation in the model... if you'll need to re-use a complete model method elsewhere.

For any post-processing of data, it definitely makes sense to do at the last possible moment before it goes into the DB though. I'm already doing that in the model.

It looks like something that seems to be down to personal preference, without any real recommendations, and that's a good thing as I can do it the way it makes sense to me.