CodeIgniter Forums
how to make the email not unique when editing - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: how to make the email not unique when editing (/showthread.php?tid=59917)



how to make the email not unique when editing - El Forum - 12-10-2013

[eluser]cerberus478[/eluser]
I've created a form where the person can add users to the database.
It has a name field and an email field.
The email field has the "is_unique" option if the form_validation rules.

What I would like is when a person has to edit the users details that the email field doesn't display the "email must be unique" error message.

users controller
Code:
function get_data_from_post(){
         $data['name'] = $this->input->post('name', TRUE);
         $data['email'] = $this->input->post('email', TRUE);
         $data['password'] = $this->input->post('password', TRUE);
         return $data;

     }

     function get_data_from_db($update_id){
         $query = $this->mdl_users->get_where($update_id);
         foreach ($query->result() as $row){
             $data['name'] = $row->name;
          $data['email'] = $row->email;
          $data['password'] = $row->password;
         }
        
         if(!isset($data)){
             $data = "";
         }
  
         return $data;

     }

     function edit(){

        
         $update_id = $this->uri->segment(3);
         $submit = $this->input->post('submit', TRUE);
        
         if($submit=="Submit"){
             $data = $this->get_data_from_post();
         } else {
             if(is_numeric($update_id)){
                 $data = $this->get_data_from_db($update_id);
             }
         }
        
         if(!isset($data)){
             $data = $this->get_data_from_post();
         }
        
         $data['update_id'] = $update_id;
        
         $data['view_file'] = "edit";
         $this->load->module('templates');
         $this->templates->admin($data);
     }

     function submit(){
         $this->load->library('form_validation');

  
   $this->form_validation->set_rules('name', 'Name', 'trim|required');
   $this->form_validation->set_rules('email', 'Email', 'trim|required|is_unique[users.email]|valid_email');
   $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
   $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');

                 if ($this->form_validation->run($this) == FALSE)
                     {
                         $this->edit();
                     }else{
                        
                         $data = $this->get_data_from_post();
                        
                    
                        
                         $update_id = $this->uri->segment(3);
                             if(is_numeric($update_id)) {
                                
                                 $this->mdl_users->_update($update_id, $data); //goes to the _update function
        } else {
                                 $this->mdl_users->_insert($data); //else it goes to the _insert function
                             }

                         redirect('users/manage');
                     }
                 }

mdl_users model
Code:
class Mdl_users extends CI_Model {

protected $_table_name = "users";
protected $_primary_key = "id";
protected $_primary_filter = "intval";
protected $_order_by = "order";
protected $_timestamps = "FALSE";

function __construct() {
  parent::__construct();
}




function get_where($id){
$table = $this->get_table();
$this->db->where('id', $id);
$query=$this->db->get($this->_table_name);
return $query;
}

function get_where_custom($col, $value) {
$table = $this->get_table();
$this->db->where($col, $value);
$query=$this->db->get($table);
return $query;
}

function _insert($data){
$table = $this->get_table();
$this->db->insert($this->_table_name, $data);
}

function _update($id, $data){
$table = $this->get_table();
$this->db->where('id', $id);
$this->db->update($table, $data);
}

edit view
Code:
<h2>Create a page</h2>
&lt;?php
    

    echo validation_errors("<p  red;'>", "</p>");
    
    echo form_open('users/submit/'.$update_id);
?&gt;
<table width="900" cellpadding="8" cellspacing="0" border="1">
    <tr>
        <td> Name:
            &lt;?php
                $data = array(
                    'name' => 'name',
                    'id' => 'name',
                    'value' => $name,
                    'maxlength' => '230',
                    'size' => '50',
                    'style' => 'width: 320px;',
                );
                
                echo form_input($data);
            ?&gt;
        </td>
     </tr>
    
    <tr>
        <td colspan="2"> Email:
            &lt;?php
                $data = array(
                    'name' => 'email',
                    'id' => 'email',
                    'value' => $email,
                    'maxlength' => '230',
                    'size' => '50',
                    'style' => 'width: 800px;',
                );
                
                echo form_input($data);
            ?&gt;
        </td>
    </tr>

  
    <tr>
    
        <td colspan="2" id="content"> Password:
            &lt;?php
                $data = array(
                    'name' => 'password',
                    'id' => 'password',
                    'value' => $password,
                    'maxlength' => '230',
                    'size' => '50',
                    'style' => 'width: 700px;',
                );
                
                echo form_input($data);
            ?&gt;
        </td>
    </tr>

    <tr>
    
        <td colspan="2" id="content"> Confirm Password:
            &lt;?php
                echo form_input('password2', 'Password Confirm');
            ?&gt;
        </td>
    </tr>
    
    <tr>
        <td colspan="2" align="center">
            &lt;?php
                echo form_submit('submit', 'Submit');
            ?&gt;
        </td>
    </tr>
</table>

&lt;?php
    echo form_close();



how to make the email not unique when editing - El Forum - 12-10-2013

[eluser]Otemu[/eluser]
Hi,

You could check if in edit mode and apply a rule

if(!$editmode){
$this->form_validation->set_rules('email', 'Email', 'trim|required|is_unique[users.email]|valid_email');
}else{
//do something else
}


how to make the email not unique when editing - El Forum - 12-10-2013

[eluser]cerberus478[/eluser]
Thanks for the reply. I'm not sure how I would go about incorporating the !$editmode


how to make the email not unique when editing - El Forum - 12-10-2013

[eluser]noideawhattotypehere[/eluser]
When adding new entry this is good, but when editing that sucks. Well the best way would be to write a callback function for a validator which checks if record with that mail exists but with other id than the one being edited.