I have made a Registration and Login application with Codeigniter 3.
The Signup form validation makes sure that every email in the users table is unique, as illustrated bellow:
Instead of "The Email field must contain a unique value" below the e-mail field, I want a flash message to let the user know the provided email already exists and that he/she should sigin in instead of signing up:
My
signup() function looks like this:
Code:
public function signup()
{
$this->form_validation->set_rules('first_name', 'First name', 'required');
$this->form_validation->set_rules('last_name', 'Last name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[users.email]');
$this->form_validation->set_rules('password', 'Password', 'required|matches[cpassword]');
$this->form_validation->set_rules('cpassword', 'Confirm password', 'required');
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
if ($this->form_validation->run()) {
$first_name = $this->input->post('first_name');
$last_name = $this->input->post('last_name');
$email = $this->input->post('email');
$password = $this->input->post('password');
$date_created = date('Y-m-d H:i:s');
$date_updated = date('Y-m-d H:i:s');
$verification_key = md5($email);
$active = 0;
$this->load->model('Usermodel');
// Create account
if ($this->Usermodel->user_register($first_name, $last_name, $email, $password, $date_created, $date_updated, $verification_key, $active)) {
$this->session->set_flashdata("signup_sucess", "Your account was created. Your have to activate it before you can signin. We have send you an activation email at $email.");
} else {
$this->session->set_flashdata("signup_failure", "We ware unable to create your account");
}
redirect('signup');
} else {
$this->load->view('signup');
if ($email_exists) {
$this->session->set_flashdata("email_exists", "The email address you provided already exists. Please signup.");
}
}
}
I am using an
$email_exists function inside an if statement but the function itself does not exist
yet.
I want to make use of CI's
is_unique inside the $email_exists function. How can I do this?
Is this a good approach to achieving the desired functionality?