views:

41

answers:

1

Hi there!

I have a problem that I can't figure out.

I'm building a small framework (practice) and now I'm building the Validator library. everything works superb except when I trying to fix the "callback" rule. Callback rule is used when the dev need to use a rule that dosen't exists in the Validator lib..

Here's how I'm doing it.

In the controller:

$this->validator->check('req|callback:test', $_POST['module_name'], 'Module Name');

Validator Class:

public function check($rules, $input, $fieldname='') {
    $rule = explode('|', $rules);
    foreach ($rule as $r) {
        $request = explode(":",$r,2);
        $method = array_shift($request);
        $parameter = $request;
        array_unshift($parameter,$input);   
        array_push($parameter,$fieldname);
        if (method_exists($this, $method)) {
            call_user_func_array(array($this,$method), $parameter);
        }
  }

protected function callback($input, $key, $fieldname='') {

}

My problem is that, how can I use the "current" controller. For example, In this case I need to find:

UserController::test();

I can find the right method in $key but is the a simple way to figure out wich controller that's rolling?

I tried to use the url like:

$controller = ucfirst($uri->uri[1]).'Controller';
$controller::$key();

But that didn't work at all.. Any ideas?

A: 

A few different ways to approach this.

First, I assume that somewhere you have some code that turns a URI

example.com/foo

into a controller classname

class FooController

This is typically called "routing". If so, somehow expose your router class/method to the validator and/or the entire system. This will let you, at anytime, get the name of a controller from the URI.

Second, when instantiating your validator, give it a reference to the controller object for the particular request. Then it's just a simple call to get_class_name or the reflection API away.

Third, require that your controller be passed along with your call to the check method.

Alan Storm
I got it to work now and I can validate the callback from the current controller BUT, when I try to set an error message like: $this->validator->set_error("Callback error messsage");It do not get saved in the error array in the validator class. Why's that?
Tobias
Solved it with call_user_func() :) Thanks anyway.
Tobias