![]() |
Where to put form callback functions? - 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: Where to put form callback functions? (/showthread.php?tid=19670) Pages:
1
2
|
Where to put form callback functions? - El Forum - 06-15-2009 [eluser]heat23[/eluser] I am using CI 1.7.1 (PHP4) and was wondering how to put form validation callback functions in a helper or library? Basically, I want a centralized place to put all validations other than the current controller. Is this possible? Where to put form callback functions? - El Forum - 06-15-2009 [eluser]gtech[/eluser] yes its possible Just create your function in the library or helper and then you can load it in the controller and call the function from the controller. If you don't want to load the helper/library each time you can autoload it in the config/autoload.php file. This will make your function available in every controller and model. Where to put form callback functions? - El Forum - 06-15-2009 [eluser]heat23[/eluser] So I simply define the callback function as previously done, like: $rules['username'] = "callback_username_check"; And in my custom validations library just create a function, like: function username_check($str) { //do something } And load this library in my controller class? Where to put form callback functions? - El Forum - 06-15-2009 [eluser]gtech[/eluser] yep I see no reason why that would not work.. you can call functions with a variable name in php, I assume this works in php4.. i suppose theres always eval if not. [url="http://uk.php.net/manual/en/functions.variable-functions.php"]variable functions[/url] Where to put form callback functions? - El Forum - 06-15-2009 [eluser]heat23[/eluser] I am having trouble setting up a library for this... Does it need to extend anything? Do need a constructor? Where to put form callback functions? - El Forum - 06-15-2009 [eluser]gtech[/eluser] I maybee misunderstanding what you require.. but here is how I would call a function from a call back variable. Test Controller Code: <?php system\application\libraries\Callback.php Code: <?php this enables you to pass a function name as a string to any function and call the callback class where you like... again if you autoload the callback library then this callback class will be available globally throughout your app. Where to put form callback functions? - El Forum - 06-15-2009 [eluser]heat23[/eluser] Ok I created a custom class and put it in the library directory. In there, I created a function like this: Code: function username_check($str) When I submit my form, it is calling this function because I see the "-----", however it is not setting the error message like it is supposed to. Any ideas? Where to put form callback functions? - El Forum - 06-15-2009 [eluser]heat23[/eluser] I am using the form validation library for the callback: $rules['username'] = "callback_username_check"; $this->validation->set_rules($rules); Where to put form callback functions? - El Forum - 06-15-2009 [eluser]Evil Wizard[/eluser] How does the newly created library know that the validation callback is for it? I know it's possible to have callback functions outside of the current object scope but then the call to the function and the object it resides in is explicitly specified, i.e. Code: class My_callback { Code: class My_callbacklibrary { Where to put form callback functions? - El Forum - 06-15-2009 [eluser]gtech[/eluser] I get what your after now! sorry I didn't understand you were using the form validators own callback mechanism... sorry for wasting your time.. If your still stuck I will have a play after din dins. |