在我的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头('content-type')='application/json'和通过post发送的数据作为json对象,如:{"id":"1","manager":"2","department":"sales","level":"3"}
我看不出有什么问题,但我可以建议在调试中,如果你调用:
echo validation_errors();
它经常给你一些有用的信息。