Welcome Guest, Not a member yet? Register   Sign In
Where to put form callback functions?
#1

[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?
#2

[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.
#3

[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?
#4

[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]
#5

[eluser]heat23[/eluser]
I am having trouble setting up a library for this... Does it need to extend anything? Do need a constructor?
#6

[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
class Test extends Controller {
  function index()
  {
    $callback = 'testd';
    $callbackdata = 'data';

    $this->load->library('Callback');
    $this->callback->$callback($callbackdata);
  }
}
?>

system\application\libraries\Callback.php
Code:
<?php
class Callback {
  function testd($data)
  {
    echo $data;
  }
}
?>

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.
#7

[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)
    {
        echo "------";
        $CI =& get_instance();
        $CI->validation->set_message('username_check','You must enter a username');
                return false;
        }

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?
#8

[eluser]heat23[/eluser]
I am using the form validation library for the callback:

$rules['username'] = "callback_username_check";
$this->validation->set_rules($rules);
#9

[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 {
    public static function array_index($foo)
    {
        return implode('', $foo);
    }
}
$bar = array_map(array('My_callback', 'array_index'), array(1,2,3,4,'test', 'foo' => 'bar'));
but I don't know how you would tell the validation library to use an alternative object than the controller, unless the validation rules are being set in the library then at that point then library object becomes the one responsible for the call back. I think the object that sets the validation rules will be the object that can house the rule callback function.
Code:
class My_callbacklibrary {
    public function set_rules()
    {
        $CI = get_instance() // object are passed by reference, no need for the =&
        $CI->form_validation->set_rules(array('field'=>'test','label'=>'My Test','rules'=>'trim|callback_username_check'));
    }
    // the scope of the method does not matter as the self object will be calling the callback
    // so the method will always be in scope
    protected function username_check($username)
    {
        echo $username;
    }
}
Hope that helps a little
#10

[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.




Theme © iAndrew 2016 - Forum software by © MyBB