[eluser]TheFuzzy0ne[/eluser]
Hi everyone.
I was concerned about the way custom validation was handled after realising that the callback functions are public in the controller. That is unless you call upon the callback function from within the rules as callback__myMethod (note the 2 underscores).
This didn't seem very consistent to me, and whilst the function would most likely harmless if run externally, I still believe it's something we could do without. Here's a section of system/libraries/Validation.php, which I have edited slightly to allow for better consistency without breaking anyone's current apps.
Code:
//Line 243
/*
* Set the current field
*
* The various prepping functions need to know the
* current field name so they can do this:
*
* $_POST[$this->_current_field] == 'bla bla';
*/
$this->_current_field = $field;
// Cycle through the rules!
foreach ($ex As $rule)
{
// Is the rule a callback?
$callback = FALSE;
if (substr($rule, 0, 9) == 'callback_')
{
$rule = substr($rule, 9);
# Start edit
/**
* Check to see if the callback function exists. If
* it doesn't, assume we need to prefix the function
* name with an underscore
*/
$rule = (method_exists($this->CI, $rule)) ? $rule : "_$rule";
# End edit
$callback = TRUE;
}
//Line 272
Note that I have only really added one line. It checks to see if the function exists, and if it doesn't, it assumes the method name should be prefixed with an underscore. So now you can hide your private controller functions and not have to use callback__someFunction, although that method will still work.
This may not be the most elegant solution, but it's probably the most straight forward way of achieving the results I was after.
I hope someone finds this useful. Personally I'd like to see this added to the next release. It's hardly bloat, and it gives the programmer more freedom to do what they want to do.
Rather than hacking the core, I would suggest copying the file from system/libraries, to application/libraries and editing it there.