Welcome Guest, Not a member yet? Register   Sign In
Callback email check failing
#1

[eluser]Barwick[/eluser]
Trying to set-up a call back function, but it's not working for some reason. Not sure what I'm doing wrong here.

Look for the registration() and _email_check($str) functions in the controller below.

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Register extends CI_Controller {

public function __construct()
{
    parent::__construct();
    $this -> load -> model('user_model');
}

public function index()
{
    if (($this -> session -> userdata('user_name') != "")) {
        redirect('dashboard');
    }
    else
    {
        $data['title'] = 'Register';
        $this -> load -> view('shared/header_view', $data);
        $this -> load -> view("registration.php", $data);
        $this -> load -> view('shared/footer_view', $data);
    }
}

public function login()
{
    $email = $this -> input -> post('email');
    $password = $this -> input -> post('pass');

    $result = $this -> user_model -> login($email, $password);
    if ($result)
        redirect('dashboard');
    else
        $this -> index();
}

public function registration()
{
    $this -> load -> library('form_validation');
    // field name, error message, validation rules
    $this -> form_validation -> set_rules('user_name', 'User Name', 'trim|required|min_length[4]|xss_clean');
    $this -> form_validation -> set_rules('email_address', 'Your Email', 'trim|required|valid_email', 'callback__email_check');
    $this -> form_validation -> set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
    $this -> form_validation -> set_rules('con_password', 'Password Confirmation', 'trim|required|matches[password]');

    if ($this -> form_validation -> run() == FALSE)
    {
        $this -> index();
    }
    else
    {
        $this -> user_model -> add_user();

        $email = $this -> input -> post('email');
        $password = $this -> input -> post('pass');            

        $result = $this -> user_model -> login($email, $password);
        if ($result)
        {
            redirect('dashboard');  
        }          
    }        
}

// Checks that Email Address is not in use
public function _email_check($str)
{
    // return true if the address is indeed a new address
    $this -> db -> where('email', $str);
    $found = $this -> db -> get('user') -> num_results(); // this returns the number of rows having the same address.

    if ($found!=0)
    {
        $this -> form_validation -> set_message('email_check', 'Email Address is already in use');
        return false;  // more than 0 rows found. the callback fails.
    }
    else
    {
        return true;   // 0 rows found. callback is ok.
    }
}

public function logout()
{
    $newdata = array('user_id' => '', 'user_name' => '', 'user_email' => '', 'logged_in' => FALSE, );
    $this -> session -> unset_userdata($newdata);
    $this -> session -> sess_destroy();
    redirect(base_url());
}
}

Here's the MODEL in case you need it:

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Model {

public function __construct() {
    parent::__construct();
}

function login($email, $password)
{
    $this -> db -> where("email", $email);
    $this -> db -> where("password", $password);

    $query = $this -> db -> get("user");
    if ($query -> num_rows() > 0)
    {
        foreach ($query->result() as $rows)
        {
            //add all data to session
            $newdata = array(
                'user_id' => $rows -> id,
                'user_name' => $rows -> username,
                'user_email' => $rows -> email,
                'logged_in' => TRUE,
            );
        }
        $this -> session -> set_userdata($newdata);
        return true;
    }
    return false;
}

public function add_user()
{
    $data = array(
        'username' => $this -> input -> post('user_name'),
        'email' => $this -> input -> post('email_address'),
        'password' => $this -> input -> post('password'),
    );
    $this -> db -> insert('user', $data);
}
}
#2

[eluser]LuckyFella73[/eluser]
I didn't check the rest of your code - what I found is that
you didn't call the callback function the right way.

Code:
// instead of this
$this -> form_validation -> set_rules('email_address', 'Your Email', 'trim|required|valid_email', 'callback__email_check');

// do this  
$this -> form_validation -> set_rules('email_address', 'Your Email', 'trim|required|valid_email|callback__email_check');

The rule "callback__anycallbackfunction" has to be set like any other
rule and not as additionally paramter.
#3

[eluser]Barwick[/eluser]
Duh! You're right! Still new to this CI stuff...However, now I'm getting an error:

Fatal error: Call to undefined method CI_DB_mysql_result::num_results() in /public_html/website.com/application/controllers/register.php on line 70

Which is this line here...not sure what it means:
Code:
$found = $this -> db -> get('user') -> num_results(); // this returns the number of rows having the same address.

Not seeing anything wrong in my model above...agh!
#4

[eluser]CroNiX[/eluser]
As the error indicates, there is no method named "num_results()" in the database class. I believe you want num_rows().
#5

[eluser]LuckyFella73[/eluser]
Try this:

Code:
public function _email_check($str)
{
  // return true if the address is indeed a new address
  $this -> db -> where('email', $str);
  $query = $this -> db -> get('user');

  if ($query -> $query->num_rows() > 0)
  {
   $this -> form_validation -> set_message('email_check', 'Email Address is already in use');
   return false;  // more than 0 rows found. the callback fails.
  }
  else
  {
   return true;   // 0 rows found. callback is ok.
  }
}
#6

[eluser]LuckyFella73[/eluser]
Remeber that database interaction in general belongs to the model.

A bit better:
Code:
// put this in your model
public function email_in_use($str)
{
  // return true if the address is indeed a new address
  $this -> db->where('email', $str);
  $query = $this->db->get('user');

  if ($query->num_rows() > 0)
  {
   $this->form_validation->set_message('email_check', 'Email Address is already in use');
   return false;  // more than 0 rows found. the callback fails.
  }
  else
  {
   return true;   // 0 rows found. callback is ok.
  }
}

// callback function in your controller
public function _email_check($str)
{
  return $this->user_model->email_in_use($str);
}
#7

[eluser]CroNiX[/eluser]
@lucky you used num_results() there too Smile
#8

[eluser]LuckyFella73[/eluser]
lol - I first overlooked that one but in my second post
I didn't correct it ...

But I edited it - thanks for the hint!
#9

[eluser]Barwick[/eluser]
Thanks for the help guys! I actually ended doing pretty much what you suggested LuckyFella. Line for line actually. You rock...




Theme © iAndrew 2016 - Forum software by © MyBB