Welcome Guest, Not a member yet? Register   Sign In
Strange validation error message issue
#1

[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;
    }
    
}

?&gt;

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)Wink 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
#2

[eluser]Armchair Samurai[/eluser]
You've mislabeled your callbacks in the set_message() function, they need to be the same as the function. Therefore, you need:

Code:
$this->form_validation->set_message('email_check', 'The e-mail address is already registered.');

not

Code:
$this->form_validation->set_message('email', 'The e-mail address is already registered.');

for the email_check callback.
#3

[eluser]majidmx[/eluser]
Hi,
The code for setting the error message should be :
Code:
$this->form_validation->set_message('email_check', 'The e-mail address is already registered.');
and the other should be :
Code:
$this->form_validation->set_message('display_check', 'The display name is already in use.');
The error message's name should be identical to function's name.
Take care,

MajiD Fatemian
#4

[eluser]newbrand[/eluser]
Ohhhhh, I thought I would pass the field name, but it totally makes sense to pass the function name. I've reviewed the User Guide thinking that it was mistaken but this is actually noted in the following section of Settings Error Messages - "In the "callback" example above, the error message was set by passing the name of the function".

Doh! My bad. Perhaps the User Guide could be tweaked to make it clearer. Undecided

Another quick question I have is in display_check, will the value passed to the function be passed through the xss_clean filter and the other modifiers before being given to the display_check function? or should I be doing some manual clean-up within the function itself before sending to the database?
#5

[eluser]Armchair Samurai[/eluser]
Validation rules are processed in order, so since callback_display_check is the final rule, all "clean up" will have been done by the time the callback is processed.




Theme © iAndrew 2016 - Forum software by © MyBB