CodeIgniter Forums
Separating Code / Error Handling - 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: Separating Code / Error Handling (/showthread.php?tid=40287)



Separating Code / Error Handling - El Forum - 04-05-2011

[eluser]Unknown[/eluser]
I am new to CodeIgniter but am having this problem quite a bit. Say I have a controller 'Contacts' and the index() page lists all the contacts... and on this same page I want to have a form to add a contact... The only thing I can do (If I want to print validation errors), is to handle the form in the same controller...

Is there a way of making a new method in the controller... add_contact().. handling the form in that then passing the errors back to the index() controller? Or is this just not a good idea?


Separating Code / Error Handling - El Forum - 04-05-2011

[eluser]wh1tel1te[/eluser]
Sure, there is no reason why you can't handle the form validation in another method. In your index() method, just call the add_contact() method, which will return your errors. If you don't want people to be able to access "index.php/contacts/add_contact", you can add an underscore before the "add_contact" method to make it private, so it becomes "_add_contact()".


Separating Code / Error Handling - El Forum - 04-05-2011

[eluser]Unknown[/eluser]
Ahh fantastic (: thanks. I didn't realise you could use _ to stop direct access.


Separating Code / Error Handling - El Forum - 04-05-2011

[eluser]InsiteFX[/eluser]
The _method in CodeIgniter makes that method private.

You can also do $_var_name

InsiteFX