1

在我的 REST 应用程序中,我试图将一个 json 对象发送到我的 API 服务器,并且服务器必须验证数据以确保每件事都按预期进行。

在 API 服务器上进行验证的控制器

$this->load->model('api/central');
$this->load->library('form_validation');

//validation rules
$this->form_validation->set_error_delimiters('', '');
$this->form_validation->set_rules('id', 'id', 'required|numeric');
$this->form_validation->set_rules('manager', 'manager', 'required|numeric');
$this->form_validation->set_rules('department', 'department', 'required');
$this->form_validation->set_rules('level', 'level', 'required|numeric');


if ($this->form_validation->run() === FALSE) {

    $employeeId = $this->form_validation->error('id') ? $this->form_validation->error('id') : $this->post('id');
    $manager = $this->form_validation->error('manager') ? $this->form_validation->error('manager') : $this->post('manager');
    $department = $this->form_validation->error('department') ? $this->form_validation->error('department') : $this->post('department');
    $level = $this->form_validation->error('level') ? $this->form_validation->error('level') : $this->post('level');

    $response = array(
        'status' => FALSE,
        'error' => 'We don\'t have data to display, Minimum information required to process the request is missing',                
        'authenticated' => TRUE,                
        'id' => $employeeId,
        'manager' => $manager,
        'department' => $department,
        'level' => $level                
    );

    $this->response($response, 200);

} else {

    $response = $this->central->calls_get($this->post('id'), $this->post('manager'), $this->post('department'), $this->post('level'));
    $this->response($response, 200);
}

返回提交的值的问题$this->post('id'), $this->post('manager'), $this->post('department'), $this->post('level'),但 form_validation 库始终无法验证数据,尽管它按预期发送

以下是一些有用 的 http 信息 http-header ('content-type') = 'application/json' 以及通过 post 作为 json 对象发送的数据,例如:{"id":"1", "manager":"1 ", "部门":"销售", "级别":"1"}

4

1 回答 1

0

我看不出有什么问题,但我可以建议调试,如果你打电话给:

echo validation_errors();

它通常会为您提供一些有用的信息。

于 2013-09-28T02:38:30.980 回答