• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nice way let user know email already exists via a flash message

#1
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:

[Image: whXGF.png]

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:

[Image: UpQXi.png]

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?
Reply

#2
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
       }
   }
Reply

#3
(11-11-2017, 08: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.
Reply

#4
Go into system folder languages/english copy the files and put them into the App/language folder edit as you please.
Reply

#5
(11-12-2017, 06:34 PM)ChicagoPhil Wrote: Go into system folder languages/english copy the files and put them into the App/language folder edit as you please.

That is a very useful piece of information but, like I specified, this should be a flash message. And one more thing: it should only concern the existing email issue.
Reply

#6
I think you can do this without a redirect and eliminate the need for flashdata. But it depends on the view html and on how you are creating the "flash message" markup. Care to share your view code?
Reply

#7
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.");

Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.