is_unique for codeigniter form validation

Using your code as an example, the is_unique validation rule works by looking for a field called user_name in your users database table. If the field with the same value exists it validates as false.

To make sure it runs only when the user submits a new value, you could check the posted value $this->input->post('user_name') against the value you pulled from the database to populate your form with. If they are the same, don’t validate is_unique;

if($this->input->post('user_name') != $original_value) {
   $is_unique="|is_unique[users.user_name]"
} else {
   $is_unique=""
}

$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean'.$is_unique);

Leave a Comment