Would Like Feedback on Form_Validation Tweak |
[eluser]volition23[/eluser]
Hello -- I just recently discovered that I could share code among several different applications using the /third_party directory. Along that line, I wanted to share callback functions for validating common elements across applications (such as a phone number) so that I wouldn't have to copy the same functions from one Controller to another across apps. So I have the following file in my /third_party directory: /some.application/third_party/shared/helpers/phone_number_helper.php I found that once I removed the validation callbacks from my Controller and instead tried to load them from the above helper, nothing happened. Not even an error was thrown. So I dug into the CodeIgniter Form_validation library to see what was going on. I found the reason for this behavior in the _execute() function: if (!method_exists($this->CI, $rule)) { continue; } When the callbacks are defined in the Controller, they are considered to be members of $this->CI. However if they're defined in some other file, they aren't. So I changed the above check to this: if(method_exists($this->CI, $rule)) { $result = $this->CI->$rule($postdata, $param); } elseif(function_exists($rule)) { $result = $rule($postdata, $param); } else continue; I'm just curious if anyone who is more familiar with the guts of the framework than I sees any problems with this. Thanks.
[eluser]Aken[/eluser]
Personally, if I want a custom rule that I want to share throughout an application, I will extend the Form_validation library and just add a method to it. Then call it like any other included rule (without the "callback_" prefix). I can't think of anything specific that might be detrimental from doing what you're doing, aside from possibly making it more difficult to upgrade to future versions of CodeIgniter.
[eluser]volition23[/eluser]
Aken -- Thank you very much for your advice, which I adopted. Should you care to see the next issue I encountered with loading my extended Form_validation library from the third_party folder, read the following: http://ellislab.com/forums/viewthread/210284/ Thanks again. |
Welcome Guest, Not a member yet? Register Sign In |