• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Form Validation Callback Not Working on Server (but OK on WAMP)

#1
Hi all,

I've developed a CI site on my local machine using WAMP. I'm using CI 3 with the HMVC extension and it all works fine.

I've just uploaded the site to the production server and changed the config files etc to get it working. However, form validation callbacks are not working on the production server.

Here's an example from a login form:

PHP Code:
// Process login form
        public function submit()
        {
            $this->load->library('form_validation');
            $username $this->input->post('username'TRUE);
            $this->form_validation->set_rules('username''Username''required');
            $this->form_validation->set_rules('password''Password''required|callback_do_login');
            
            
if($this->form_validation->run($this) == FALSE)
            {
                $this->login();
            }
            else 
            {
                // Set login session
                
                
...
            }
        


This is the callback function do_login:

PHP Code:
public function do_login($password)
        {
            
            
// Get user / pass from POST
            $submitted_password $this->input->post('password'TRUE);
            $username $this->input->post('username'TRUE);
            
            $this
->crud->setTable($this->table);
            $this->load->model('mdl_users');
            
            
// Check User Exists
            $query $this->mdl_users->getUser($username);
            if($query->num_rows() == 1)
            {
                // Get stored password
                $row $query->row();
                $stored_password $row->password;
                
                
// Check password
                $this->load->module('mod_security');
                $result $this->mod_security->login($username$submitted_password$stored_password); // Returns false if no match
                
                
return ($result) ? TRUE FALSE;
            }
            else
            {
                
                
return FALSE;
            }
            
        



On my local WAMP setup, this all works fine. But on the server
PHP Code:
$this->form_validation->run($this
always returns false.

To eliminate any errors with the callback function itself, I changed it to the following as a test:

PHP Code:
public function do_login($password)
{
 
     return TRUE;


... which will obviously always return TRUE, however when $this->form_validation->run($this) is called, even though I changed the do_login callback function to return TRUE, $this->form_validation->run($this) still returns FALSE!!

This is driving me crazy and I have no idea why this is happening. It is as if the form validation is ignoring the callback function and just returning false.

Can anyone help me? Could it be a setting on the server causing it or something else that could have changed when I uploaded the site files to the server?

If it is relevant, on my local machine, within the do_login callback function I had originally set the callback error message like this:


PHP Code:
$this->form_validation->set_message('do_login''User / Pass Incorrect'); 


...which worked fine, but on the production server it threw an error stating:  "Unable to access an error message corresponding to your field name Password.(do_login)".  I had to set the error message in the language file to overcome this. But the fact that this happen on the production server and not on my WAMP setup makes me think there must be some setting or something on the server that is causing this.

Anyway, any help is gratefully received.

Thanks
Reply

#2
Why are you passing $this to run()?
Reply

#3
Did you create and use the MY_Form_valdation.php file for HMVC Callbacks in the libraries folder?

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

class 
MY_Form_validation extends CI_Form_validation
{
    public function 
run($module ''$group '')
    {
        (
is_object($module)) AND $this->CI =& $module;

        return 
parent::run($group);
    }


What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#4
Yes I had created the MY_Form_validation file.

After much searching and hair pulling, I finally found a solution in the comments of a youtube video! I'll post is here in case anyone else has the same problem.

I needed to add the line 'public $CI;' to that file to get it to work on the server. I installed my CI site on 2 different servers and both had the same problem with the callbacks, even though the callbacks were working on my local WAMP setup.

Anyway, the full file becomes:



PHP Code:
<?php

class MY_Form_validation extends CI_Form_validation {

public 
$CI;

function 
run($module ''$group ''){
(
is_object($module)) AND $this->CI = &$module;
return 
parent::run($group);
}



Thanks for your suggestions anyway.

Now I have a problem with sessions disappearing on the server, but that's another story...
Reply

#5
Right, I posted the main one from my libraries for that I backup I must not had updated it,
but yes you need to have public $CI; I have updated my libraries backup to reflect this now.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.