2015-08-19 37 views
0

我有一個codeigniter表單,它運行一些基本驗證並將數據提交給數據庫。但我想額外改變其中一個字段的發佈數據,以便在提交到數據庫之前使用inflector助手將發佈的數據轉換爲camel案例。我該怎麼做呢?使用變形助手轉換提交的表單數據在codeigniter中

這是我目前的形式:

<?php echo form_open('instances/create') ?> 

    <label for="content">Content</label> 
    <textarea name="content"></textarea><br /> 


    <input type="submit" name="submit" value="Create" /> 

</form> 

這裏是我的電流控制器:

public function create(){ 
    $this->load->helper('form'); 
    $this->load->library('form_validation'); 
    $this->load->helper('inflector'); 

    $data['title'] = 'Create an instance'; 

    $this->form_validation->set_rules('title', 'Title', 'required'); 

    //want to camelize the 'title' here 

    if ($this->form_validation->run() === FALSE) 
    { 
     $this->load->view('templates/header', $data); 
     $this->load->view('instances/create'); 
     $this->load->view('templates/footer'); 

    } 
    else 
    { 
     $this->instances_model->set_instances(); 
     $this->load->view('instances/success'); 
    } 
} 

,這裏是我的模型:

<?php 
class Instances_model extends CI_Model { 

    public function __construct(){ 
     $this->load->database(); 
    } 

    public function get_instances($slug = FALSE){ 
     if ($slug === FALSE){ 
      $query = $this->db->get('extra_instances'); 
      return $query->result_array(); 
     } 

     $query = $this->db->get_where('extra_instances', array('slug' => $slug)); 
     return $query->row_array(); 
    } 

    public function set_instances(){ 
     $this->load->helper('url'); 

     $slug = url_title($this->input->post('title'), 'dash', TRUE); 

     $data = array(
      'slug' => $slug, 
      'title' => $this->input->post('title'), 
      'content' => $this->input->post('content'), 
      'year' => $this->input->post('year'), 
      'credit' => $this->input->post('credit'), 
      'source' => $this->input->post('source') 
     ); 

     return $this->db->insert('extra_instances', $data); 
    } 
} 

我知道,你可以camelize變量與以下:

echo camelize('my_dog_spot'); // Prints 'myDogSpot' 

,我知道,你可以這樣運行了自定義的驗證:

$this->form_validation->set_rules('username', 'Username', 'callback_username_check'); 

public function username_check($str) 
     { 
       if ($str == 'test') 
       { 
         $this->form_validation->set_message('username_check', 'The {field} field can not be the word "test"'); 
         return FALSE; 
       } 
       else 
       { 
         return TRUE; 
       } 
     } 

但我缺乏的是如何把這個完全提交到數據庫之前迅速改變POST數據的知識。

+0

我沒有看到你的代碼的一部分,你實際存儲數據,你做到了嗎? – Avalanche

+0

抱歉 - 將模型添加到上面的代碼中 – mheavers

回答

0

沒有什麼太複雜,你可以做到這一點,你通過驗證後,只需將您的data陣列進入數據庫之前:

$data = array(
    'slug' => $slug, 
    'title' => camelize($this->input->post('title')), 
    // ...