REST Api验证的问题

时间:2016-05-31 16:45:26

标签: php codeigniter rest

您好我正在使用CodeIgniter构建REST API。问题是我已设置验证规则但代码无法识别它们。我正在使用https://github.com/chriskacerguis/codeigniter-restserver

方法放置:https://github.com/alexmarton/RControl/blob/master/application/controllers/api.php,此示例有效。但就我而言,事实并非如此。

public function properties_put(){
    $property_to_update = $this->uri->segment(3);
    $this->load->model('Model_properties');
    $this->load->library('form_validation');
    if (isset($property_to_update)) {
        if (is_numeric($property_to_update)) {
            $property_exist = $this->Model_properties->get_by(array('ID'=> $property_to_update));
            if ($property_exist) {
                $data = remove_unknown_fields($this->put(), $this->form_validation->get_field_names('property_put'));
                $this->form_validation->set_data($data);
                $debugdata = $this->form_validation->get_field_names('property_put');

                foreach ($debugdata as $key => $value) {
                    log_message('debug', "Found validation data (".($key+1).")" . $value);

                }
                foreach ($data as $k => $val) {
                    log_message('debug', "Unknown field data (".($k+1).")" . $val);

                }

                if ($this->form_validation->run('property_put') != false) {
                    log_message('debug', "Passed validation data ");
                }else{
                    $this->response(array("status" => "failure", "status_code" => "400", "response" => $this->form_validation->get_errors_as_array() ), REST_Controller::HTTP_BAD_REQUEST);
                    log_message('debug', "Error in validation data ");
                }
            } else {
                $this->response(array("status" => "failure" , "status_code" => "404" , "message" => "Not Found", "response"=>"We couldn't find a property with the specified :id"), REST_Controller::HTTP_NOT_FOUND);
            }
        }

    } else {
        $this->response(array("status" => "failure" , "status_code" => "422" , "message" => "Unprocessable Entity", "response"=>"You have to specify the :id or the :name of the property that you would like to edit/update"), REST_Controller::HTTP_UNPROCESSABLE_ENTITY);
    }



}

应用/ form_validation:

$config = array(
 'price_post' => array(
   array( 'field' => 'property_id', 'label' => 'Property id', 'rules' => 'trim|required' ),
   array( 'field' => '_from', 'label' => 'Timeframe from', 'rules' => 'trim|required' ),
   array( 'field' => '_to', 'label' => 'Timeframe to', 'rules' => 'trim|required' ),
   array( 'field' => 'price', 'label' => 'Price', 'rules' => 'trim|required|integer|min_length[2]|is_natural_no_zero' ),
  ),
 'availability_post' => array(
   array( 'field' => 'property_id', 'label' => 'Property id', 'rules' => 'trim|required' ),
   array( 'field' => '_from', 'label' => 'Timeframe from', 'rules' => 'trim|required' ),
   array( 'field' => '_to', 'label' => 'Timeframe to', 'rules' => 'trim|required' ),
   array( 'field' => 'free', 'label' => 'Is it free or not', 'rules' => 'trim|required|integer|numeric' )
  ),
 'image_post' => array(
   array( 'field' => 'property_id', 'label' => 'Property id', 'rules' => 'trim|required' ),
   array( 'field' => 'url', 'label' => 'Url', 'rules' => 'trim|required|valid_url|prep_url' ),
   array( 'field' => 'sort_order', 'label' => 'Sort Order', 'rules' => 'trim|required' )
  ),
 'property_put' => array(
   array( 'field' => 'name', 'label' => 'Property Name', 'rules' => 'trim|required' ),
   array( 'field' => 'village', 'label' => 'Property Village', 'rules' => 'trim|required' ),
   array( 'field' => 'town', 'label' => 'Property Town', 'rules' => 'trim|required' ),
   array( 'field' => 'province', 'label' => 'Property Province', 'rules' => 'trim|required' ),
   array( 'field' => 'region', 'label' => 'Property Region', 'rules' => 'trim|required' ),
   array( 'field' => 'type', 'label' => 'Property Type', 'rules' => 'trim|required' )
  )

);

应用/助手/ my_api_helper:

function remove_unknown_fields($form_fields, $expected_fields){
    $new_data = array();
    foreach ($form_fields as $key => $value) {
        if ($value != "" && in_array($key, array_values($expected_fields))) {
            $new_data[$key] = $value; 
        }       
    }
    return $new_data;
}

应用/库/ MY_Form_validation:

class MY_Form_validation extends CI_Form_validation {

 function __construct($rules = array()) {
    parent::__construct($rules);
    $this->ci =& get_instance();
 }

 public function get_errors_as_array() {
    return $this->_error_array;
 }

 public function get_config_rules() {
    return $this->_config_rules;
 }

 public function get_field_names($form) {
    $field_names = array();
    $rules = $this->get_config_rules();
    $rules = $rules[$form];
    foreach ($rules as $index => $info) {
      $field_names[] = $info['field'];
    }
    return $field_names;
 }

}

调试信息:

DEBUG - 2016-05-31 18:34:25 --> Found validation data (1)name
DEBUG - 2016-05-31 18:34:25 --> Found validation data (2)village
DEBUG - 2016-05-31 18:34:25 --> Found validation data (3)town
DEBUG - 2016-05-31 18:34:25 --> Found validation data (4)province
DEBUG - 2016-05-31 18:34:25 --> Found validation data (5)region
DEBUG - 2016-05-31 18:34:25 --> Found validation data (6)type
DEBUG - 2016-05-31 18:34:25 --> Unable to find validation rules

并且在我不发布数据时仍然不会显示错误。任何人都可以帮我理解发生了什么?

1 个答案:

答案 0 :(得分:0)

你需要做的是:

像这样修改property_put方法:

public function properties_put(){
    $property_to_update = $this->uri->segment(3);
    $this->load->model('Model_properties');
    $this->load->library('form_validation');
    if (isset($property_to_update)) {
        if (is_numeric($property_to_update)) {
            $property_exist = $this->Model_properties->get_by(array('ID'=> $property_to_update));
            if ($property_exist) {
                $property = $this->Model_properties->update_by('primary_field_key', $property_to_update, array(
                                                      'your_field_1' => $this->put('field_1'),
                                                      'your_field_2' => $this->put('field_2')
                                                      ));
                if($property){
                  //display correct response 
                } else {
                  //display wrong response
                }
            } else {
                $this->response(array("status" => "failure" , "status_code" => "404" , "message" => "Not Found", "response"=>"We couldn't find a property with the specified :id"), REST_Controller::HTTP_NOT_FOUND);
            }
        }

    } else {
        $this->response(array("status" => "failure" , "status_code" => "422" , "message" => "Unprocessable Entity", "response"=>"You have to specify the :id or the :name of the property that you would like to edit/update"), REST_Controller::HTTP_UNPROCESSABLE_ENTITY);
    }
}

为了使上述工作,数据必须具有以下内容:

标题:Content-type: application/json 如果您使用的是Postman,则必须使用raw作为JSON,如此

{
"field_1":"value 1",
"field_2":"value 2"
}

希望它有所帮助!!!