【问题标题】:Kohana 3.3 Validation during updateKohana 3.3 更新期间的验证
【发布时间】:2013-10-15 08:47:02
【问题描述】:

我正在使用 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

请帮忙!

【问题讨论】:

    标签: php kohana-3


    【解决方案1】:

    错误已经给了你解决方案。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-02
      • 1970-01-01
      • 2012-10-30
      • 1970-01-01
      • 2011-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多