简体   繁体   English

REST Api验证的问题

[英]Problemm with REST Api Validation

Hello i am building a REST API with CodeIgniter. 您好我正在使用CodeIgniter构建REST API。 The problemm is that i have set the validation rules but the code does not recognise them. 问题是我已设置验证规则但代码无法识别它们。 I am using https://github.com/chriskacerguis/codeigniter-restserver . 我正在使用https://github.com/chriskacerguis/codeigniter-restserver

The method put: https://github.com/alexmarton/RControl/blob/master/application/controllers/api.php and this example works. 方法放在: https//github.com/alexmarton/RControl/blob/master/application/controllers/api.php这个例子有效。 But in my case it does not. 但就我而言,事实并非如此。

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);
    }



}

application/form_validation: 应用程序/ 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' )
  )

);

application/helpers/my_api_helper: 应用程序/佣工/ 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;
}

application/libraries/MY_Form_validation: 应用/库/ 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;
 }

}

The debug info: 调试信息:

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

and still it does not display errors when i do not post data. 并且当我不发布数据时它仍然不显示错误。 Anyone can help me understand what is going on? 任何人都可以帮我理解发生了什么?

What you have to do is this: 你要做的是:

Modify the property_put method like this: 像这样修改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);
    }
}

In order for the above to work put data must have the following: 为了使上述工作,放置数据必须具备以下条件:

Header: Content-type: application/json If you are using Postman you have to use raw as JSON like this 标题: Content-type: application/json如果你使用Postman,你必须像这样使用raw作为JSON

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

Hope it helps!!! 希望能帮助到你!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM