php - Codeigniter: Validate form field on edit -
i having trouble , since more week trying find solution disallow duplicate form content if exists in database.
so check rows excluding id (row) editing , if same value exists should give error message.
here code.
position controller
public function position_edit($id = null) { $this->data['title'] = '<i class="fa fa-user"></i> ' . lang('position_edit'); $this->data['position'] = $this->positions_model->get($id); count($this->data['position']) || $this->data['errors'][] = 'position not found'; $id = $this->uri->segment(4); $this->db->where('position', $this->input->post('position')); !$id || $this->db->where('id !=', $id); $pos = $this->positions_model->get(); echo '<pre>', print_r($pos), '</pre>'; if (count($pos) > 0) { $this->form_validation->set_rules('position', 'lang:position_code', 'trim|required|max_length[10]|is_unique[positions.position]|xss_clean'); $this->form_validation->set_message('is_unique', lang('error_position_exists')); } if ($this->form_validation->run() === true) { $data = $this->positions_model->array_from_post(array('position', 'label')); $this->positions_model->save($data, $id); $this->session->set_flashdata('message', lang('position_record_updated')); $this->data['message'] = $this->session->flashdata('message'); $this->session->set_flashdata('message_type', 'success'); $this->data['message_type'] = $this->session->flashdata('message_type'); //redirect('admin/hr/positions', 'refresh'); } // load view $this->load->view('hr/positions/edit', $this->data); }
position model
class positions_model extends my_model { protected $_table_name = 'positions'; protected $_order_by = 'label asc'; // $rules not in use since has been // set directly controller edit method code public $rules = array( 'position' => array( 'field' => 'position', 'label' => 'position code', 'rules' => 'trim|required|max_length[10]|xss_clean' ), 'label' => array( 'field' => 'label', 'label' => 'position label', 'rules' => 'trim|required|max_length[50]|xss_clean' ), ); public function get_new() { $position = new stdclass(); $position->position = ''; $position->label = ''; return $position; } public function get_positions($id = null, $single = false) { $this->db->get($this->_table_name); return parent::get($id, $single); } public function get_positions_array($id = null, $single = false) { $this->db->get($this->_table_name); $positions = parent::get($id, $single); $array = array(); foreach($positions $pos){ $array[] = get_object_vars($pos); } return $array; } public function delete($id) { // delete position parent::delete($id); } }
db model
class my_model extends ci_model { protected $_table_name = ''; protected $_primary_key = 'id'; protected $_primary_filter = 'intval'; protected $_order_by = ''; public $rules = array(); protected $_timestamps = false; function __construct() { parent::__construct(); } public function array_from_post($fields) { $data = array(); foreach ($fields $field) { $data[$field] = $this->input->post($field); } return $data; } public function get($id = null, $single = false) { if($id != null) { $filter = $this->_primary_filter; $id = $filter($id); $this->db->where($this->_primary_key, $id); $method = 'row'; } elseif($single == true) { $method = 'row'; } else { $method = 'result'; } if(!count($this->db->ar_orderby)) { $this->db->order_by($this->_order_by); } return $this->db->get($this->_table_name)->$method(); } public function get_by($where, $single = false) { $this->db->where($where); return $this->get(null, $single); } public function save($data, $id = null) { // set timestamps if ($this->_timestamps == true) { $now = date('y-m-d h:i:s'); $id || $data['created'] = $now; $data['modified'] = $now; } // insert if ($id === null) { !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = null; $this->db->set($data); $this->db->insert($this->_table_name); $id = $this->db->insert_id(); } else { // update $filter = $this->_primary_filter; $id = $filter($id); $this->db->set($data); $this->db->where($this->_primary_key, $id); $this->db->update($this->_table_name); } return $id; } public function delete($id) { $filter = $this->_primary_filter; $id = $filter($id); if (!$id) { return false; } $this->db->where($this->_primary_key, $id); $this->db->limit(1); $this->db->delete($this->_table_name); } }
i have tried callback function not working @ , couldn't find causing issue.
edit:
please note it above code giving message if inserting value exists not validating , storing data if row not exists
updated
if ($this->form_validation->run() === true) { //print_r($this->positions_model->unique_value('position', $this->uri->segment(4))); if($this->positions_model->unique_value('position', $this->uri->segment(4))) { $this->form_validation->set_message('unique_value', lang('error_position_exists')); } else { $data = $this->positions_model->array_from_post(array('position', 'label')); $this->positions_model->save($data, $id); $this->session->set_flashdata('message', lang('position_record_updated')); $this->data['message'] = $this->session->flashdata('message'); $this->session->set_flashdata('message_type', 'success'); $this->data['message_type'] = $this->session->flashdata('message_type'); redirect('admin/hr/positions', 'refresh'); } }
in controller
public function unique_value($field, $id) { $id = $this->uri->segment(4); $this->db->where($field, $this->input->post($field)); !$id || $this->db->where('id !=', $id); $position = $this->positions_model->get(); if (count($position)) { return true; } return false; }
please note: don't know happens code, wouldn't check between insert or update model, in controller
i solve using checkexist model function, check if values want check exists ddbb, , work according it. in controller instead of model. first, validate fields, , check if values exists excluding edit id:
$values_from_post = $this->positions_model->array_from_post(array('position', 'label')); // $editid avoid id of row editing if ($this->form_validation->run() === true ) { if ( !$this->positions_model->check_duplicate( $values_from_post, $editid ) ) ){ // insert value } else { // update value }
and in model, check duplicate via if values exists:
public function check_duplicate( $values_from_post, $editid ) { foreach ( $values_from_post $key => $value ) { $this->db->where( $key, $value ); } $this->db->where('id !=', $editid ); $result = $this->db->get($this->_table_name); return ( ( $result->num_rows > 0 ) ? true : false ); }
please, didn't check code, idea, instead of doing in model, control in controller , insert or update depending of happens.
Comments
Post a Comment