为什么我在使用回调函数时正在使用的页面中获得404

时间:2009-08-20 07:51:50

标签: php kohana

这是控制器功能寄存器()

public function register()
{
        $userdata = array();
        $formdata = array(
            'uname' => '',
            'password' => '',
            'email' => '',
            'fname' => '',
            'lname' => ''
        );

        // copy the form as errors
        $errors = $formdata;

        // process data if form is submitted
        if (isset($_POST['submitted']))
        {
            // initialize validation library
            $post = new Validation($_POST);

            // add filters
            $post->pre_filter('trim', 'uname', 'fname', 'lname');

            // set validation rules
            $post->add_rules('uname', 'required', 'alpha_numeric');
            $post->add_rules('password', 'required', 'length[6, 12]');
            $post->add_rules('email', 'required', 'email');
            $post->add_rules('fname', 'required', 'alpha');
            $post->add_rules('lname', 'required', 'alpha');
            $post->add_callbacks('uname', array($this, ' _username_exists'));

            if($post->validate())
            {
                $userdata = array(
                    'username' => $_POST['uname'],
                    'email' => $_POST['email'],
                    'password' => $_POST['password'],
                    'firstname' => $_POST['fname'],
                    'lastname' => $_POST['lname']
                );

                $this->user->register($userdata);
                // url::redirect('test/register_success');
            }
            // show form with validation errors
            else
            {
                // repopulate form fields
                $formdata = arr::overwrite($formdata, $post->as_array());

                // populate the error fields
                $errors = arr::overwrite($errors, $post->errors('register_error_messages'));
            }
        }

        // show form
        $view = new View('register');
        $view->form = $formdata;
        $view->errors = $errors;
        $view->render(true);
    }

这是回调函数

public function _username_exists(Validation $post)
    {
        // if add_rules validation found any errors, exit
        if (array_key_exists('uname', $post->errors())) return;

        $result = $this->user->check_username_exists($username);
        if (!$result)
        {
            //add error to validation object
            $post->add_error('uname', 'username_exists');
        }
    }

如果我注释掉对回调函数的调用

,它的效果非常好

1 个答案:

答案 0 :(得分:1)

add_callback方法中的方法名称字符串中有一个空格。

$post->add_callbacks('uname', array($this, ' _username_exists'));

$post->add_callbacks('uname', array($this, '_username_exists'));

我认为这会解决问题。