0

我正在使用 Kohana 3.3 并想在我的控制器中验证用户输入,但它返回以下错误:

ErrorException [ 警告 ]: call_user_func_array() 期望参数 1 是有效的回调,第二个数组成员不是有效的方法

这是我的控制器:

$this->template->user =Auth::instance()->get_user();

                $courseModel = Model::factory('courses');

                $object = Validation::factory($this->request->post());
                // $object->bind(':model', $courseModel);
                $object
                    ->rule('code', 'not_empty')
                    ->rule('code', 'Model_Courses::unique_code')
                    ->rule('code', array('max_length', array(':value', 32)))
                    ->rule('description', 'not_empty');

                if($object->check()) { //this is where the error triggers
                    $user = ORM::factory('courses', $this->request->param('id'))
                        ->values($_POST, array(
                            'code', 
                            'description', 
                        ));

                    $query = DB::update('courses')
                        ->set(array(
                            'code' => $_POST['code'], 
                            'description' => $_POST['description'],
                            ))
                        ->where('id', '=', $this->request->param('id'));

                    $result = $query->execute();

                    // Reset values so form is not sticky
                    $_POST = array();

                    $courses = ORM::factory('courses')
                        ->find_all();
                    $json = array();

                    foreach ($courses as $course) {
                        if($course->id != 1) $json[] = $course->as_array();
                    }
                    $data = json_encode($json);

                    // Display users table
                    $courseView = View::factory('courses/list');
                    $courseView->bind('content', $data);

                    $this->template->content = $courseView;

我的 Model_Courses 代码如下:

class Model_Courses extends ORM {
protected $_table_name = 'courses';
protected $_primary_key = 'id';

public function rules() {
    return array(
        'code' => array(
            array('not_empty'),
            array('max_length', array(':value', 32)),
            array(array($this, 'unique'), array(':field', ':value')),
        ),
        'description' => array(
            array('not_empty'),
        ),
    );
}

public static function unique_code($code)
{
    return ! DB::select(array(DB::expr('COUNT(code)'), 'total'))
        ->from('courses')
        ->where('code', '=', $code)
        ->execute()
        ->get('total');
}

}

我错过了什么?我在这里遵循了文档:

http://kohanaframework.org/3.3/guide/kohana/security/validation

请帮忙!

4

1 回答 1

0

该错误已经为您提供了解决方案。

call_user_func_array() 期望参数 1 是有效的回调,第二个数组成员不是有效的方法

  • call_user_func_array()是一个调用用户函数(谁想)的函数。这种情况在您的代码中唯一发生在规则中,

    array(array($this, 'unique'), array(':field', ':value')),

    在这里,您想通过名称作为字符串调用用户函数。

  • 现在它说第一个参数应该是一个有效的回调。关键字exptected告诉您,在您的代码中并非如此。
  • 最后一部分,不是一个有效的方法正是导致问题的原因。

你的方法标题是

public static function unique_code($code)

所以名字不是unique但是unique_code

array(array($this, 'unique'), array(':field', ':value')), // fails
array(array($this, 'unique_code'), array(':field', ':value')), // should work
于 2013-10-15T13:34:28.097 回答