我有一个codeigniter表单,它运行一些基本验证并将数据提交到数据库。但我想另外更改其中一个字段的post数据以使用inflector helper,以便在提交到数据库之前将发布的数据转换为camel case。我该怎么做?
这是我目前的表格:
<?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);
}
}
我知道您可以通过以下方式对变量进行跟踪:
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 :(得分:0)
没有什么太复杂的,你可以在传递验证之后,在将data
数组插入数据库之前完成:
$data = array(
'slug' => $slug,
'title' => camelize($this->input->post('title')),
// ...