php - 如何使用 form_validation->set_rules() 在 Codeigniter 中创建 is_unique 验证,这将在更新时起作用?
问题描述
如何在 CI 中创建一个可以基于唯一值进行更新的验证规则?我在我的控制器中创建了一个函数来在表中添加一些值。
/**
* Function to create and update a smart pixel
* @return json
*/
public function create()
{
$this->form_validation->set_rules('pixel_name', 'pixel name', 'required');
$this->form_validation->set_rules('pixel_id', 'pixel id', 'required');
if ($this->form_validation->run() == FALSE)
{
$temp = array();
$temp['header'] = $this->lang->line('smart_pixel_create_validation_error_heading');
$temp['content'] = $this->lang->line('smart_pixel_create_validation_error_message') . validation_errors();
$this->return = array();
$this->return['responseCode'] = 0;
$this->return['responseHTML'] = $this->load->view('shared/error', array('data'=>$temp), TRUE);
die(json_encode($this->return));
}
/** Checking for unique pixel name while adding a pixel */
if($this->input->post('field_id') == '' && $this->input->post('pixel_name') != ''){
$err = [];
$err['header'] = $this->lang->line('smart_pixel_create_validation_error_heading');
$err['content'] = $this->lang->line('smart_pixel_create_pixel_name_validation_error_message') . validation_errors();
$check_pixel_name = $this->MPixels->check_pixel_name();
if($check_pixel_name){
$this->return = array();
$this->return['responseCode'] = 0;
$this->return['responseHTML'] = $this->load->view('shared/error', array('data'=>$err), TRUE);
die(json_encode($this->return));
}
}
/** Checking for unique pixel name while update and updating the pixel information */
if($this->input->post('field_id') != ''){
$check_for_update = $this->MPixels->check_pixel_name_update($this->input->post('field_id'));
if($check_for_update){
$update_response = $this->MPixels->update();
$success = [];
$success['header'] = $this->lang->line('smart_pixel_create_success_heading');
$success['content'] = $this->lang->line('smart_pixel_update_success_message');
$this->return = array();
$this->return['responseCode'] = 1;
$this->return['responseHTML'] = $this->load->view('shared/success', array('data' => $success), TRUE);
die(json_encode($this->return));
}else{
$err = [];
$err['header'] = $this->lang->line('smart_pixel_create_validation_error_heading');
$err['content'] = $this->lang->line('smart_pixel_create_pixel_name_validation_error_message') . validation_errors();
$this->return = array();
$this->return['responseCode'] = 0;
$this->return['responseHTML'] = $this->load->view('shared/error', array('data'=>$err), TRUE);
die(json_encode($this->return));
}
}
/** Function to insert the smart pixel in the pixels table */
$pixel_id = $this->MPixels->insert();
if($pixel_id){
$success = [];
$success['header'] = $this->lang->line('smart_pixel_create_success_heading');
$success['content'] = $this->lang->line('smart_pixel_create_success_message');
$this->return = array();
$this->return['responseCode'] = 1;
$this->return['responseHTML'] = $this->load->view('shared/success', array('data' => $success), TRUE);
die(json_encode($this->return));
}
}
现在在我的模态中,我已经制作了那些用于验证的功能,我制作了一个功能
/**
* Check the pixel name exist in table or not while create
* @return boolean
*/
public function check_pixel_name()
{
$this->db->where('user_id', $this->session->userdata('user_id'));
$this->db->where('user_pixel_name', $this->input->post('pixel_name'));
$q = $this->db->get('pixels');
if($q->num_rows() > 0){
return TRUE;
}else{
return FALSE;
}
}
/**
* Check the pixel name exist in table or not while update
* @param id
* @return boolean
*/
public function check_pixel_name_update($id = ''){
$this->db->where('id', $id);
$this->db->where('user_id', $this->session->userdata('user_id'));
$this->db->where('user_pixel_name', $this->input->post('pixel_name'));
$q = $this->db->get('pixels');
if($q->num_rows() > 0){
return TRUE;
}else{
/** check for unique pixel name */
$this->db->where('user_id', $this->session->userdata('user_id'));
$this->db->where('user_pixel_name', $this->input->post('pixel_name'));
$q = $this->db->get('pixels');
if($q->num_rows() > 0){
return FALSE;
}else{
return TRUE;
}
}
}
所有这些功能都可以工作,但是如何使用 CI 表单验证功能来实现。
感谢您回答这个问题。希望你在这个野兔时期做得很好。
解决方案
推荐阅读
- javascript - 如何在不使用本地网络服务器的情况下读取 chrome 中的本地文本文件?
- usb - libusbdotnet 找不到 RFIDeas USB HID 设备
- python - 将 python 脚本转换为函数以遍历每一行
- codeigniter - CodeIgniter - 无法访问控制器文件
- node.js - 使用 nodejs lib 在 elasticsearch 上执行原始查询
- c# - 当任何参数为空时,模型变为空
- ruby-on-rails - 模型的 ActiveResource 多个嵌套路由
- json - 如何将模型转换为 JSON
- android - WorkManager alpha10 ListenableFuture 用法
- r - 根据条件将值替换到上面的行