Kohana 3.3 更新期间的验证


Kohana 3.3 Validation during update

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

错误异常 [ 警告 ]: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

请帮忙!

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

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