-
Ajax30
Member
-
Posts: 71
Threads: 46
Joined: Aug 2017
Reputation:
0
11-11-2017, 05:33 AM
(This post was last modified: 11-11-2017, 05:59 AM by Ajax30.)
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?
-
agus
Newbie
-
Posts: 3
Threads: 0
Joined: Nov 2017
Reputation:
0
according to : user_guide/libraries/form_validation.html#setting-validation-rules in documentation if using v3
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]',
array('is_unique' => 'The email address %s provided already exists. Please signup.'));
$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() == FALSE) {
//$message show validation error
$message = validation_errors();
//return error in $message
} else {
//insert code if no error
}
}
-
Ajax30
Member
-
Posts: 71
Threads: 46
Joined: Aug 2017
Reputation:
0
11-12-2017, 02:48 PM
(This post was last modified: 11-12-2017, 02:49 PM by Ajax30.)
(11-11-2017, 07:27 AM)agus Wrote: according to : user_guide/libraries/form_validation.html#setting-validation-rules in documentation if using v3
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]',
array('is_unique' => 'The email address %s provided already exists. Please signup.'));
$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() == FALSE) {
//$message show validation error
$message = validation_errors();
//return error in $message
} else {
//insert code if no error
}
}
I am asking this: how can I replace "The Email field must contain a unique value." with "The email address you provided already exists. Please signup"? In addition, this should be a flash message.
-
averroez
Newbie
-
Posts: 4
Threads: 0
Joined: Oct 2017
Reputation:
0
11-14-2017, 12:18 AM
(This post was last modified: 11-14-2017, 01:59 AM by averroez.)
As @ ChicagoPhil said you can translate/change the messages and then you can use the helpers like form_error('email') to render the message where ever you like. if you really need to test if email has errors change your $email_exists statement like so
PHP Code: if ($this->form_validation->error('email')) { $this->session->set_flashdata("email_exists", "The email address you provided already exists. Please signup."); }
|