CodeIgniter Forums

Full Version: Form Validation Always Errors
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
I am trying to use a callback with the Form Validation Class. The callback method checks to see if a given username exists. By using log_message, I can see that the callback returns true or false as it should. Yet, when I submit my form, the callback method always generates an error: the value of the $lang['_user_exists'] (E-mail address already used.) I am so stumped... anyone have a clue for me?

members controller

Code:
public function register()
{
$location = $this->config->item('base_url') . 'members/';
$this->is_authenticated ? header('Location: ' . $location) : null;

$this->load->helper(array('url', 'form'));
$this->load->library('form_validation');

$this->form_validation->set_error_delimiters(
  $this->config->item('error_delimiter_start', 'user'),
  $this->config->item('error_delimiter_end', 'user')
);

if ($this->form_validation->run() === true) {
  $email = $this->input->post('email_address');
  $password = $this->input->post('password');
  $additional_data = array(
   'first_name' => trim($this->input->post('first_name')),
   'last_name' => trim($this->input->post('last_name')),
   'preferred_name' => trim($this->input->post('preferred_name'))
  );

  if ($this->user->register($email, $password, $additional_data)) {
   $location = $this->config->item('base_ssl') . 'members/signin';
    header('Location: ' . $location);
   }
  }
  $data['array_snipped'];
  $this->load->view('members/register', $data);
}
}

public function _user_exists($email_address)
{
$this->output->enable_profiler(TRUE);
// returns true if a given email address already exists
$exists = $this->user->user_exists($email_address);
if ($exists === true) {
  log_message('debug', 'user::user_exists() returned true');
  log_message('debug', 'members::_user_exists() returning false');
  return false; // for form validation callback
} elseif ($exists === false) {
  log_message('debug', 'user::user_exists() returned false');
  log_message('debug', 'members::_user_exists() returning true');
  return true; // for form validation callback
}
return $exists;
}

users class library

Code:
public function user_exists($email)
{
return $this->ci->user_model->user_exists($email);
}

users model

Code:
public function user_exists($email)
{
$this->db->where($this->tables['users'].'.email', $email);
$this->db->limit(1);

$i = $this->db->get($this->tables['users']);
log_message('debug', 'user_model::user_exists() num rows found: ' . $i->num_rows);

if ($i->num_rows > 0) {
  log_message('debug', 'user_model::user_exists() returning true');
  return true;
} else {
  log_message('debug', 'user_model::user_exists() returning false');
  return false;
}
}

form_validation config file
Code:
$config = array(
'members/register' => array(
  array(
   'field' => 'first_name',
   'label' => 'lang:members_input_first_name',
   'rules' => 'trim|required|min_length[2]|max_length[255]'
  ),
  array(
   'field' => 'last_name',
   'label' => 'lang:members_input_last_name',
   'rules' => 'trim|required|min_length[2]|max_length[255]'
  ),
  array(
   'field' => 'preferred_name',
   'label' => 'lang:members_input_preferred_name',
   'rules' => 'trim|required|min_length[2]|max_length[255]'
  ),
  array(
   'field' => 'email_address',
   'label' => 'lang:members_input_email_address',
   'rules' => 'trim|required|valid_email|min_length[8]|max_length[255]|callback__user_exists'
  ),
  array(
   'field' => 'password',
   'label' => 'lang:members_input_password',
   'rules' => 'trim|required|min_length[9]|max_length[99]' // PCI compliant
  ),
  array(
   'field' => 'password_verify',
   'label' => 'lang:members_input_password_verify',
   'rules' => 'trim|required|min_length[9]|max_length[99]|matches[password]' // PCI compliant
  )
)
);