[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>
<?php
echo validation_errors("<p red;'>", "</p>");
echo form_open('users/submit/'.$update_id);
?>
<table width="900" cellpadding="8" cellspacing="0" border="1">
<tr>
<td> Name:
<?php
$data = array(
'name' => 'name',
'id' => 'name',
'value' => $name,
'maxlength' => '230',
'size' => '50',
'style' => 'width: 320px;',
);
echo form_input($data);
?>
</td>
</tr>
<tr>
<td colspan="2"> Email:
<?php
$data = array(
'name' => 'email',
'id' => 'email',
'value' => $email,
'maxlength' => '230',
'size' => '50',
'style' => 'width: 800px;',
);
echo form_input($data);
?>
</td>
</tr>
<tr>
<td colspan="2" id="content"> Password:
<?php
$data = array(
'name' => 'password',
'id' => 'password',
'value' => $password,
'maxlength' => '230',
'size' => '50',
'style' => 'width: 700px;',
);
echo form_input($data);
?>
</td>
</tr>
<tr>
<td colspan="2" id="content"> Confirm Password:
<?php
echo form_input('password2', 'Password Confirm');
?>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<?php
echo form_submit('submit', 'Submit');
?>
</td>
</tr>
</table>
<?php
echo form_close();