Search code examples
codeignitercallbackhmvc

Codeigniter & HMVC - Callback not working


I've pretty much gone through a lot of link and solutions provided here and other locations, but I am just not able to solve the callback issue that I am facing. I'm using Codeigniter with HMVC the code is below.

The following code is from My_Form_validation.php:

class MY_Form_validation extends CI_Form_validation {

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

Below if the Callback function :

public function _unique_email($str) {

    // Check if user already exists
    // Process only for current user
    $id = $this->uri->segment(4);
    $this->db->where('email', $this->input->post('email'));
    !$id || $this->db->where('id !=', $id);
    $user = $this->mdl_admin_users->get();

    if (count($user)) {
        $this->form_validation->set_message('_unique_email', 'User already exists. Please check %s.');
        return FALSE;
    }

    return TRUE;
}

and the function :

public function user_edit($id = NULL) {

    // Fetch a user or set a new one
    if ($id) {
        $data['user'] = $this->mdl_admin_users->get($id);
        count($data['user']) || $data['errors'][] = 'User could not be found';
    }
    else {
        $data['user'] = $this->mdl_admin_users->get_new();
    }

    // setup the form
    $rules = $this->mdl_admin_users->rules_admin;
    $id || $rules['password'] = '|required';
    $this->form_validation->set_rules($rules);

    //process the form
    if ($this->form_validation->run($this) == TRUE) {
        $data = $this->mdl_admin_users->array_from_post(array('firstname', 'lastname', 'email', 'password'));
        $data['password'] = $this->mdl_admin_users->hash($data['password']);
        $this->mdl_admin_users->save($data, $id);
        redirect('admin/user');
    }

    // Load the view
    $data['title'] = 'Edit Users';
    $data['module'] = 'admin';
    $data['header_file'] = 'header_admin';
    $data['nav_file'] = 'nav_admin';
    $data['view_file'] = 'edit_users';
    $data['footer_file'] = 'footer_admin';
    echo Modules::run('template/base_template', $data);

}

Would be a great help if someone could point me at the right direction to resolve the issue. Thanks in advance

Naveen


Solution

  • First of you are missing in the rules

    $rules['email'] = 'required|callback__uniqueemail';

    Also call back function should be not like this callback__unique_email for some reason I found codeigniter call back not like extra gap this better callback__uniqueemail

    If private do not work make public function removing underscore

    public function uniqueemail() // no need $str
    

    When make public do not for get to remove extra underscore from here callback_uniqueemail

    Another thing with echo Modules run best to be loaded from view only.

    In your controller replace echo Modules run with $this->load->view();

    You need to add $this->form_validation->run($this) add $this to run after create library below.

    And Create a new library

    <?php
    
    class MY_Form_validation extends CI_Form_validation {
    
        function run($module = '', $group = '') {
            (is_object($module)) AND $this->CI = &$module;
            return parent::run($group);
        }
    
    } 
    

    Tutorial Best https://www.youtube.com/watch?v=8fy8E_C5_qQ