[eluser]newbrand[/eluser]
Hello,
I seem to be experiencing some strange issues with CI when creating my first registration script. I'm trying to modify the error message displayed on a callback function to check the availability of a display name.
Here's a quick sampling of the code:
Code:
<?php
class Register extends Controller {
function Register()
{
parent::Controller();
}
function index()
{
$this->load->library('form_validation');
$this->load->database();
$view = array();
if (isset($_POST['register']))
{
$this->form_validation->set_rules('display', 'Display Name', 'trim|required|alpha_numeric|xss_clean|max_length[20]|callback_display_check');
$this->form_validation->set_rules('email', 'E-Mail', 'trim|required|valid_email|callback_email_check');
$this->form_validation->set_rules('firstname', 'First Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('lastname', 'Last Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('pass', 'Password', 'trim|required|matches[passconfirm]');
$this->form_validation->set_rules('passconfirm', 'Confirm Password', 'trim|required');
print_r($this->form_validation);
if ($this->form_validation->run() == true)
{
// $this->db->query("INSERT INTO users SET display = ?, email = ?, firstname = ?, lastname = ?, datereg = ?, password = ?",
// array($_POST['display'],
// $_POST['email'],
// $_POST['firstname'],
// $_POST['lastname'],
// date("Y-m-d H:i:s"),
// md5($_POST['pass'])));
$this->send_registration($this->db->insert_id(), $_POST['display'], $_POST['email']);
$view['display'] = $_POST['display'];
$view['success'] = "Thank you for registering, an activation e-mail has been sent to your inbox. " .
"Follow the link in the e-mail to complete the activation process and login.";
}
}
$this->load->view('register', $view);
}
function send_registration($id, $display, $email) {
require_once 'lib/SiteMailer.php';
$mailer = new SiteMailer();
$mailer->to($_POST['email']);
$mailer->message("Dear {$display},<br><br>Thank you for registering for {SITENAME}. To complete the registration process" .
"follow this link: <a >config->item('base_url')}/activate/{$active_id}'>" .
"{$this->config->item('base_url')}/register/activate/{$active_id}</a>. Once you have activated the account" .
"successfully, you may continue to login.<br><br>Thank you,<br><br>{$this->config->item('site_name')}");
$mailer->send();
}
function email_check($email)
{
$query = $this->db->query("SELECT user_id FROM users WHERE email = '{$email}'");
if ($query->num_rows() != "0")
{
$this->form_validation->set_message('email', 'The e-mail address is already registered.');
return false;
}
return true;
}
function display_check($display)
{
$query = $this->db->query("SELECT user_id FROM users WHERE display = '{$display}'");
if ($query->num_rows() != "0")
{
$this->form_validation->set_message('display', 'The display name is already in use.');
return false;
}
return true;
}
}
?>
Pretty sure I've got everything in place but I keep getting this error message displaying: Unable to access an error message corresponding to your field name.
So I went ahead to debug it, found a posting on the forum regarding the issue from May 2008 which suggested a hack workaround to modify the reference - implemented it, didn't work. So I dug a bit deeper. If I add the message at line 59 and do a print_r() on the validation object (print_r($this->form_validation)
it displays the object accordingly with the _error_messages array in the object set with the proper value. I then do the same thing at line 23 after the validation and it appears as though the message hasn't been carried over.
Thus the message displays in the callback function but not in the main code. This seems to be an issue with references though I'm not 100% sure on how to track it down or solve it. I'm a bit curious however because this is basically the same code as in the user guide - perhaps it's a bug specific to my PHP version?
Using PHP Version 5.2.5 - any ideas for a proper way to fix it (or a hack if I get desperate)?
Loving CI, just want it to work :-p
Cheers,
Dave