2016-05-31 83 views
1

你好,我正在用CodeIgniter构建一个REST API。问题是我已经设置了验证规则,但代码无法识别它们。我正在使用https://github.com/chriskacerguis/codeigniter-restserverProblemm与REST Api验证

的方法把: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

凡产生这一行:'DEBUG - 2016年5月31日18时34分25秒 - >无法找到验证rules'? –

+1

在系统/ libraries/Form_Validation.php中它是默认的codeigniter验证类,如果没有规则,这将记录到日志中。但正如你可以看到有我 – BRG

+0

problemm发生时,我做$ this-> form_validation-> run('property_put')!= false,但这是错误的,当有错误,当有错误,它不显示它们给我 – BRG

回答

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 如果您使用邮差,您必须使用像这样的原始JSON

​​

希望它有帮助!

+0

非常感谢!已经在这里发现它[https://github.com/chriskacerguis/codeigniter-restserver/issues/362](https://github.com/chriskacerguis/codeigniter-restserver/issues/362) – BRG