我在put
REST api中执行验证,但不知何故,它一直显示false
<?php
use RestserverLibrariesREST_Controller;
defined('BASEPATH') or exit('No direct script access allowed');
require APPPATH . 'libraries/REST_Controller.php';
require APPPATH . 'libraries/Format.php';
class EventApi extends REST_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Model_basic');
$this->load->library('form_validation');
$this->load->helper('form');
}
public function Event_get()
{
$id = $this->get('id');
if ($id === null) {
$event = $this->Model_basic->select_all('px_events');
} else {
$event = $this->Model_basic->select_where('px_events', 'id', $id)->result_array();
}
if ($event) {
$this->response([
'status' => true,
'message' => $event
], REST_Controller::HTTP_OK);
} else {
$this->response([
'status' => False,
'message' => 'Id Not Found'
], REST_Controller::HTTP_NOT_FOUND);
}
}
public function Event_post()
{
//post
}
public function Event_put()
{
$id = $this->put('id');
if($id === null){
$this->response([
'status' => False,
'message' => 'Please Provide an Id'
], REST_Controller::HTTP_BAD_REQUEST);
}elseif($id ===""){
$this->response([
'status' => False,
'message' => 'Please Provide an Id'
], REST_Controller::HTTP_BAD_REQUEST);
}else{
$this->form_validation->set_rules('golf_course_id', 'golf_course_id', 'trim|required');
$this->form_validation->set_rules('title', 'title', 'trim|required');
$this->form_validation->set_rules('holes', 'holes', 'numeric|required');
$this->form_validation->set_rules('prizel_pool', 'prize_pool', 'trim|required|numeric');
$data = [
'golf_course_id' => $this->put('golf_course_id'),
'title' => $this->put('title'),
'date_start' => $this->put('date_start'),
'date_end' => $this->put('date_end'),
'holes' => $this->put('holes'),
'prize_pool' => $this->put('prize_pool'),
'date_created' => $this->put('date_created'),
'date_modified' => $this->put('date_modified')
];
if($this->form_validation->run() === false) {
$this->response([
'status' => False,
'message' => 'Please List The Field'
], REST_Controller::HTTP_BAD_REQUEST);
}else{
if ($this->Model_basic->update('px_events', $data, 'id', $id) > 0) {
$this->response([
'status' => True,
'message' => $data,
], REST_Controller::HTTP_NO_CONTENT);
} else {
$this->response([
'status' => false,
'message' => 'Update Fail',
], REST_Controller::HTTP_BAD_REQUEST);
}
}
}
}
public function Event_delete()
{
//Delete
}
表单验证默认使用POST
数据。您必须用PUT
数据填充它。 以下方法应该有效:
public function Event_put()
{
try
{
$this->form_validation->set_data($this->put());
$this->form_validation->set_rules('id', 'Id', 'trim|required');
$this->form_validation->set_rules('golf_course_id', 'golf_course_id', 'trim|required');
$this->form_validation->set_rules('title', 'title', 'trim|required');
$this->form_validation->set_rules('holes', 'holes', 'numeric|required');
$this->form_validation->set_rules('prizel_pool', 'prize_pool', 'trim|required|numeric');
if(!$this->form_validation->run()) throw new Exception(validation_errors());
$data = [
'golf_course_id' => $this->put('golf_course_id'),
'title' => $this->put('title'),
'date_start' => $this->put('date_start'),
'date_end' => $this->put('date_end'),
'holes' => $this->put('holes'),
'prize_pool' => $this->put('prize_pool'),
'date_created' => $this->put('date_created'),
'date_modified' => $this->put('date_modified')
];
if ($this->Model_basic->update('px_events', $data, 'id', $id) > 0) {
$this->response([
'status' => True,
'message' => $data,
], REST_Controller::HTTP_NO_CONTENT);
} else {
throw new Exception('Update fail');
}
}
catch(Throwable $e)
{
$this->response([
'status' => False,
'message' => $e->getMessage(),
], REST_Controller::HTTP_BAD_REQUEST);
}
}