user3142334
user3142334

Reputation: 53

creating own form validation rule in codeigniter

I am using codeigniter to build a website. There is a "forgot password" link on the login page.When the user clicks on it ,he is asked to enter his email and then will further proceed.I want to set a rule on the email entered by user to check whether it exists in the database or not.In case the email does not exist in the database an error message should be displayed that the email does not exist and redirect the user to the same page. I am new to codeigniter.Please help me.Thanks in advance.This is what I have tried with no success.

view 'change password'
<!DOCTYPE html> 
<html>
 <head>
   <title> Login</title>
   <link rel="stylesheet" href="http://localhost/cinema/assets/css/form.css">
 </head>
 <body>
 <form action="http://localhost/cinema/verifyque/sec_que" method="post" accept-charset="utf-8" class="username">
  <br>
   <p>
           <label for="email_id">Email ID</label>
       <input type="text" id="username" name="email_id"/>
     </p>

    <input type="submit" name="btnSubmit" class="styled-button-8" value="Submit"
             />
         <font color="red" size="4"><b>
         <?php echo validation_errors(); ?></b></font>

 </form></body></html>


Controller
  function sec_que(){
         $this->load->library('form_validation');
      $this->form_validation->set_rules('email_id', 'Email',    'callback_email_available');
          function email_available($str)
{
// You can access $_POST variable
$this->load->model('user');
$result =   $this->user->emailAvailability($str);
if ($result)
{
    return TRUE;
}else{
    $this->form_validation->set_message('email_available', 'The %s does not exist');
    return FALSE;
}
}
if($this->form_validation->run() === TRUE) { 
       $this->load->model('user');
        $email['email_id'] = $this->input->post('email_id');
        $this->session->set_userdata($email);
       $data['que_id_1']=  $this->user->display_que();
       $data['que_id_2']=  $this->user->display_que2();
       $this->load->view('forgot_password_2', $data);

   }
 else{
 $this->load->view('change_password');
 }
}

Model
  public function emailAvailability($email)
{
    $this->db->where('user_email',$email);
    $query  =   $this->db->get('users');
   return $query->row();
 }

Upvotes: 0

Views: 118

Answers (1)

Hubert
Hubert

Reputation: 169

Here is the code that you need to use.. Use the Callback method, Form Validation:

$this->form_validation->set_rules('Email', 'Email', 'callback_emailAvailability');

Model:

public function emailAvailability($email)
    {
        $query  = $this->db->get_where('user_email',$email);
              if($query > 0){
                return true;
               }else{
             return false;
              }
     }

Hope it helps.

Upvotes: 1

Related Questions