0

Can anyone tell me if there is a way to callback with the form_validation library in CodeIgniter to a utility function of a controller? I have a function called check_database that is a callback function set in form_validation's set_rules function...the problem is that it's a function in my controller which makes it visible to the router as a method.

I could navigate to index.php/controller/check_database and it will attempt to render a page rather than coming up with a 404 error. I've already tried making it a utility function by prepending an underscore to the function name, but the set_rules function doesn't seem to like callback__check_database.

Is there anyway I can specify the callback function as a utility function...or to hide a non-utility function from being visible to the router in CodeIgniter? This seems like something that should be entirely possible...or should I even be using form_validation in a Controller class?

Even the official guide uses a callback function that is in a Controller (http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks), so if I went to form/username_check would the controller not try to render something based on the output of that function?...it wouldn't just 404 I'm guessing.

UPDATE: Currently I have the callback set as callback__check_database and my function defined as a utility function (_check_database). This seems to work, however I can't be sure at the moment as I am briefly shown a screen that appears to have some sort of PHP error on it before it reroutes back to my login page. I'm working to find a way to capture that error now.

celestialorb
  • 1,901
  • 7
  • 29
  • 50
  • Did you try to write a private function instead of public like the example? – Maxime Morin Feb 12 '13 at 01:06
  • 2
    Odd, I've done callbacks with the underbar, and it's been fine... What exactly doesn't it like, is there an error? – Jeemusu Feb 12 '13 at 01:07
  • Actually private doesn't work, I just tried it. :( The callback is being called from a library, which obviously does not have access to the private method of the controller. I also tested @Jeemusu's solution and it does work for me. – Maxime Morin Feb 12 '13 at 01:15
  • I'm not sure the value of having the function in your controller, but if it makes sense to have it there, you can modify routes.php to redirect any calls from the outside that would otherwise invoke the method. My first choice, however, would be to move it to a utilities class that both the controller and the library could load and use. – Jerry Feb 12 '13 at 03:17
  • 1
    See [a previous answer](http://stackoverflow.com/a/12879127/568884) of mine, along with Rick's comment. Choose what works best for you. – Jordan Arsenault Feb 12 '13 at 04:34
  • Agree with Jeemusu, I've done the callbacks with underscores tons of times, as long as they're in the same controller. – Rick Calder Feb 12 '13 at 11:35
  • Oh! I see what I was doing wrong! Jeemusu is correct, you can do callbacks with a utility function, what I was (incorrectly) assuming was that you'd have to add *another* underscore to the callback tag (e.g. `callback__check_database` rather than `callback_check_database`). It works just fine using just one underscore, but breaks when using two which is what I was doing. Should I put this information into an answer? – celestialorb Feb 12 '13 at 16:42
  • Wait, no I was prematurely celebrating. It does *not* work. I'm fairly certain my utility function never gets called after I submit my form if I use the `callback_check_database` with a utility function `_check_database`. I made my utility function a normal function by removing the underscore and the site worked properly then, but it still leaves the check_database route open. :S – celestialorb Feb 12 '13 at 16:54
  • Okay, so the original idea I had to use two underscores seems to work (I had a typo in my callback function that was causing PHP to error out). I'm assuming that's why this originally did not work earlier. – celestialorb Feb 12 '13 at 17:15

0 Answers0