Codeigniter - edit form (repopulating) with edit_unique Codeigniter - edit form (repopulating) with edit_unique codeigniter codeigniter

Codeigniter - edit form (repopulating) with edit_unique


Ok - found it myself. There was no return value in case of beeing true. Perhaps anyone faces the same problem... with this function, it works:

function edit_unique($value, $params)  {    $CI =& get_instance();    $CI->load->database();    $CI->form_validation->set_message('edit_unique', "Sorry, that %s is already being used.");    list($table, $field, $current_id) = explode(".", $params);    $query = $CI->db->select()->from($table)->where($field, $value)->limit(1)->get();    if ($query->row() && $query->row()->id != $current_id)    {        return FALSE;    } else {        return TRUE;    }}


Petra's answer works well, if for some reason it doesn't work for you, check if your primary key is 'id', if not you'll need to make some changes.

Change

list($table, $field, $current_id) = explode(".", $params);if ($query->row() && $query->row()->id != $current_id)

to

list($table, $field, $current_id, $key) = explode(".", $params);if ($query->row() && $query->row()->$key != $current_id)

Then add the rule like:

$this->form_validation->set_rules(    'form_field',     'Form Label',     'required|trim|edit_unique[table.column.' . $edit . '.unique]');

Where unique is your unique/primary key and $edit is it's value.