3

So what I'm trying to do:

  1. Pull User data from database, including email address, username etc.

  2. Edit it

  3. Save it

But I want to keep username and email unique. And for this I'm setting validation rules like this:

$this->form_validation->set_rules('firstname', 'First Name', 'required|min_length[2]|max_length[15]');
$this->form_validation->set_rules('lastname', 'Last Name', 'required|min_length[2]|max_length[15]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');
$this->form_validation->set_rules('username', 'Username', 'required|min_length[4]|max_length[12]|is_unique[users.username]');
$this->form_validation->set_rules('password1', 'Password', 'required|matches[password2]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'required');
$this->form_validation->set_rules('group', 'User Group', 'required');

And as you can see I have is_unique[users.username] and is_unique[users.email] rules. But it doesn't let me update my entry using this rules.

So the question is, how can I update database entry, and keep those 2 fields unique(username and email)?

Tachi
  • 2,042
  • 3
  • 27
  • 44
  • sorry i didn't get what exactly you want, is this case works fine with create user, and not working for edit user? – Muhammad May 27 '15 at 12:25
  • tip: before update check whether **UserName** and **Mail** already exist. if exist show error else update it. Game Over – Abdulla Nilam May 27 '15 at 12:27
  • @Naeem Yes, I can create new user with unique username and email, but at the same time I can't update this user, while still keeping same email and username i've entered when created new user – Tachi May 27 '15 at 12:52
  • Have you tried behavior with other fields? – Tpojka May 27 '15 at 14:12

2 Answers2

13

use the call back validation function

$this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_check_user_email');

function check_user_email($email) {        
    if($this->input->post('id'))
        $id = $this->input->post('id');
    else
        $id = '';
    $result = $this->user_model->check_unique_user_email($id, $email);
    if($result == 0)
        $response = true;
    else {
        $this->form_validation->set_message('check_user_email', 'Email must be unique');
        $response = false;
    }
    return $response;
}

in model

    function check_unique_user_email($id = '', $email) {
        $this->db->where('email', $email);
        if($id) {
            $this->db->where_not_in('id', $id);
        }
        return $this->db->get('user')->num_rows();
    }

use same for the user name....

parth
  • 1,803
  • 2
  • 19
  • 27
0

In Codeigniter 4

// is_unique[table.field,ignore_field,ignore_value]

$validation->setRules([
 'name' => "is_unique[supplier.name,uuid, $uuid]",  // is not ok
 'name' => "is_unique[supplier.name,uuid,$uuid ]",  // is not ok
 'name' => "is_unique[supplier.name,uuid,$uuid]",   // is ok
 'name' => "is_unique[supplier.name,uuid,{uuid}]",  // is ok - see "Validation Placeholders"

]);

Eg.

$validation->setRules(['email' => 'required|valid_email|is_unique[users.email,id,4]']);
// 'id' = ignore table field name (users table 'id' field) 
// '4' = ignore field value(currnet user id)

codeigniter 4 validation

For Codeigniter 3

  1. Add following helper class (edit_unique_helper.php)

    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;
    }
    

    }

  2. Add to autoload ()

  3. Use edit_unique where you pass an extra parameter which is the id of the row you're editing eg. $this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean|edit_unique[users.user_name.'.$id.']');

Source

Prasad Gayan
  • 1,424
  • 17
  • 26