Welcome Guest, Not a member yet? Register   Sign In
Call Back Function Issue
#1

[eluser]everythingevolves[/eluser]
Hi,

I'm still quite new to code ignitor but am giving it a good go. Today I have been looking at the form_helper/validation but seem to be having an issue with the callback function. I've tried a number of different options but don't seem to be getting anywhere. The controller file is listed below:

Code:
<?php

class Register extends Controller
    {
        function Register()
            {
            parent:: Controller();
            
            $this->load->helper(array('form', 'url'));
            $this->load->library('form_validation');
            }
        
    
        function index()
            {
            $this->form_validation->set_rules('forename', 'Forename', 'trim|required|alpha_dash');
            $this->form_validation->set_rules('surname', 'Surname', 'trim|required|alpha_dash');
            $this->form_validation->set_rules('email_login', 'Email login', 'trim|valid_email|required|callback_email_login_check');
            $this->form_validation->set_rules('password', 'Password', 'trim|required|alpha_numeric|min_length[5]|max_length[12]|matches[passconf]|md5');
            $this->form_validation->set_rules('passconf', 'Confirm Password', 'trim|required');
            
            if ($this->form_validation->run() == FALSE)
                {
                $title['page_title'] = "Registration Form";
                
                $this->load->view('header', $title);
                $this->load->view('menu');
                $this->load->view('register');
                $this->load->view('related_links');
                $this->load->view('footer');
                }
            else
                {
                $data = array(
                           'forename' => $_POST['forename'],
                           'surname' => $_POST['surname'],
                        'email_login' => $_POST['email_login'],
                        'password' => $_POST['password'],
                        );
            
                $this->db->insert('users', $data);
                
                $title['page_title'] = "Registration successful!";
                $title['page_heading'] = "Your Registration was successful!";
                
                $this->load->view('header', $title);
                $this->load->view('menu');
                $this->load->view('action_successful', $title);
                $this->load->view('related_links');
                $this->load->view('footer');
                }
            }
        
        function email_login_check($email_login)
            {
            $query = $this->db->query("SELECT * FROM users WHERE email_login='$email_login'");
            
            if ($query->num_rows() > 0)
                {
                $this->form_validation->set_message('email_login_check', 'The %s "$email_login" already exists.');
                return FALSE;
                }
            else
                {
                return TRUE;
                }
            }
    }


/* End of file register.php */
/* Location: ./system/application/controllers/register.php */

When the call back is called to check that the email_login doesn't already exist in the users table of my database, I seem to keep getting this message where the error message is displayed in my view:

"Unable to access an error message corresponding to your field name."

No doubt it's something stupid that I've overlooked and I'll continue to keep trying, but if anyone can see what's wrong then it would be greatly appreciated.

All the best.
#2

[eluser]macleodjb[/eluser]
try removing the %s
Quote:$this->form_validation->set_message('email_login_check', 'The "$email_login" already exists.');
#3

[eluser]everythingevolves[/eluser]
Hi macleodjb,

Unfortunately, that still doesn't work - how annoying. I'll keep trying.

Thanks for you help though.
#4

[eluser]everythingevolves[/eluser]
I'm still having no luck - still the same message. It clearly works in that it won't let me put in the same email address twice, it just can't pull back the right message that I have declared in the set_message() function.

Code:
<?php

class Register extends Controller
    {
        function Register()
            {
            parent:: Controller();
            
            $this->load->helper(array('form', 'url'));
            $this->load->library('form_validation');
            }
        
    
        function index()
            {
            $this->form_validation->set_rules('email_login', 'Email login', 'trim|callback_email_login_check|valid_email|required');
            $this->form_validation->set_rules('forename', 'Forename', 'trim|required|alpha_dash');
            $this->form_validation->set_rules('surname', 'Surname', 'trim|required|alpha_dash');
            $this->form_validation->set_rules('password', 'Password', 'trim|required|alpha_numeric|min_length[5]|max_length[12]|matches[passconf]|md5');
            $this->form_validation->set_rules('passconf', 'Confirm Password', 'trim|required');
            
            if ($this->form_validation->run() == FALSE)
                {
                $title['page_title'] = "Registration Form";
                
                $this->load->view('header', $title);
                $this->load->view('menu');
                $this->load->view('register');
                $this->load->view('related_links');
                $this->load->view('footer');
                }
            else
                {
                $data = array(
                           'forename' => $_POST['forename'],
                           'surname' => $_POST['surname'],
                        'email_login' => $_POST['email_login'],
                        'password' => $_POST['password'],
                        );
            
                $this->db->insert('users', $data);
                
                $title['page_title'] = "Registration successful!";
                $title['page_heading'] = "Your Registration was successful!";
                
                $this->load->view('header', $title);
                $this->load->view('menu');
                $this->load->view('action_successful', $title);
                $this->load->view('related_links');
                $this->load->view('footer');
                }
            }
        
        function email_login_check($str)
            {
            $query = $this->db->query("SELECT * FROM users WHERE email_login='$str'");
            
            if ($query->num_rows() > 0)
                {
                $this->form_validation->set_message('email_login_check', 'The Email Login already exists, please try again.');
                return FALSE;
                }
            else
                {
                return TRUE;
                }
            }
    }


/* End of file register.php */
/* Location: ./system/application/controllers/register.php */
#5

[eluser]macleodjb[/eluser]
post the code from your view file where it displays the errors, i've got a feeling that something is wrong there.
#6

[eluser]everythingevolves[/eluser]
Cheers for this mate...

Code:
<div id="content_center">
<h1>Registration</h1>
&lt;?php echo validation_errors(); ?&gt;

&lt;?php echo form_open('register'); ?&gt;

<p>Forename</p>
&lt;input type="text" name="forename" value="&lt;?php echo set_value('forename'); ?&gt;" size="50" /&gt;

<p>Surname</p>
&lt;input type="text" name="surname" value="&lt;?php echo set_value('surname'); ?&gt;" size="50" /&gt;

<p>Username (email address)</p>
&lt;input type="text" name="email_login" value="&lt;?php echo set_value('email_login'); ?&gt;" size="50" /&gt;

<p>Password</p>
&lt;input type="password" name="password" value="" size="50" /&gt;

<p>Password Confirm</p>
&lt;input type="password" name="passconf" value="" size="50" /&gt;

<p>&lt;input type="submit" value="Submit" /&gt;&lt;/p>

&lt;/form&gt;
</div>

I should mention that all other errors display the right error message except for this one.
#7

[eluser]drewbee[/eluser]
While I cannot see anything off the top of my head, a few things to note here:

Callbacks should start off with a underscore (_). This makes the methods private. As it stands in your code, you can access the callback via the following url: /register/email_login_check

Change it to this:
Code:
function _email_login_check()
{
    // Also set the message accordingly:
    $this->form_validation->set_message('_email_login_check', 'ERROR with email, sir!');
}

Also,

I have never used callbacks anywhere else but as the very last item in the list of rules. This may or may not catch your problem. The callbacks area of form validation is a bit shakey at the moment in a few special cases and as such I would give it a whirl just to see what it does. Besides, why hit the database if they have not entered everything correctly in yet? The database call SHOULD be your last action regardless. (Don't forget your newly established underscore in the method name); their will be two underscores now.

Code:
$this->form_validation->set_rules('email_login', 'Email login', 'trim|valid_email|required|callback__email_login_check');

Let us know...
#8

[eluser]leafc[/eluser]
I have exactly same issue Sad Anybody any idea what's going on?
#9

[eluser]everythingevolves[/eluser]
Thanks to the both of you.

Interestingly, I took the form_validation out of the parent controller and auto loaded it globally. Instantaneously it started working. In addtion I have added the _ to the start of the method for privacy and am grateful for the tip.

How strange, again - thank you kindly both.

All the best.
#10

[eluser]everythingevolves[/eluser]
[quote author="leafc" date="1235706415"]I have exactly same issue Sad Anybody any idea what's going on?[/quote]

Try taking it out of the parent controller and auto_load it - just out of interest. I've just been messing aropund with mine basically but I'm pretty sure that's what did it. My controller now looks like this:

Code:
&lt;?php

class Register extends Controller
    {    
    
        function index()
            {
            $this->form_validation->set_rules('email_login', 'Email login', 'trim|callback_email_login_check|valid_email|required');
            $this->form_validation->set_rules('forename', 'Forename', 'trim|required|alpha_dash');
            $this->form_validation->set_rules('surname', 'Surname', 'trim|required|alpha_dash');
            $this->form_validation->set_rules('password', 'Password', 'trim|required|alpha_numeric|min_length[5]|max_length[12]|matches[passconf]|md5');
            $this->form_validation->set_rules('passconf', 'Confirm Password', 'trim|required');
            
            if ($this->form_validation->run() == FALSE)
                {
                $title['page_title'] = "Registration Form";
                
                $this->load->view('header', $title);
                $this->load->view('menu');
                $this->load->view('register');
                $this->load->view('related_links');
                $this->load->view('footer');
                }
            else
                {
                $data = array(
                           'forename' => $_POST['forename'],
                           'surname' => $_POST['surname'],
                        'email_login' => $_POST['email_login'],
                        'password' => $_POST['password'],
                        );
            
                $this->db->insert('users', $data);
                
                $this->load->library('email');

                $this->email->from('([email protected])', 'Billy Big Balls');
                $this->email->to($_POST['email_login']);

                $this->email->subject('Registation Confirmation');
                $this->email->message('This is a test of the registration confirmation email.');

                $this->email->send();

                echo $this->email->print_debugger();
                
                $title['page_title'] = "Registration successful!";
                $title['page_heading'] = "Your Registration was successful!";
                
                $this->load->view('header', $title);
                $this->load->view('menu');
                $this->load->view('action_successful', $title);
                $this->load->view('related_links');
                $this->load->view('footer');
                }
            }
        
        function _email_login_check($str)
            {
            $query = $this->db->query("SELECT * FROM users WHERE email_login='$str'");
            
            if ($query->num_rows() > 0)
                {
                $this->form_validation->set_message('_email_login_check', 'The Email Login already exists, please try again.');
                return FALSE;
                }
            else
                {
                return TRUE;
                }
            }
    }


/* End of file register.php */
/* Location: ./system/application/controllers/register.php */




Theme © iAndrew 2016 - Forum software by © MyBB