2016-05-31 84 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