0
votes

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:

// 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:

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

$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:

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:

$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 library file to overcome this. But the fact that this happened 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

1
Remove '$this' from run() method. There is no '$this' in docs. - Tpojka
The '$this' is required for the HMVC implementation - Jason

1 Answers

0
votes

I finally found the solution. The HMVC application requires a small library file named as MY_Form_validation.php. Seemingly everywhere on the net this file is shown to be thus:

class MY_Form_validation extends CI_Form_validation {

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

}

However, although this worked in my WAMP setup, it did not work on my server. I found that by declaring the CI variable at the start the server problem is resolved. So the MY_Form_validation.php file finally becomes:

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

}