Codeigniter& HMVC - 回调无法正常工作

时间:2015-01-13 13:57:47

标签: codeigniter callback hmvc

我已经在这里和其他地方提供了很多链接和解决方案,但我无法解决我所面临的回调问题。我将Codeigniter与HMVC一起使用,代码如下。

以下代码来自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);
    }
}

下面是回调函数:

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

和功能:

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

}

如果有人能指出我正确的方向来解决问题,那将是一个很大的帮助。提前致谢

纳温

2 个答案:

答案 0 :(得分:5)

根据wiredesignz,

在MX中使用表单验证时,您需要扩展CI_Form_validation类,如下所示,

/** application/libraries/MY_Form_validation **/ 
class MY_Form_validation extends CI_Form_validation 
{
    public $CI;
}

在将当前控制器作为$ CI变量分配给form_validation库之前。这将使您的回调方法正常运行。

class Xyz extends MX_Controller 
{
    function __construct()
    {
        parent::__construct();

        $this->load->library('form_validation');
        $this->form_validation->CI =& $this;
    }
}

这将删除与HMVC相关的回调问题,而不会对您的代码进行任何更改。

答案 1 :(得分:4)

你们中的第一个人在规则中缺失

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

另外回调函数应该不是这个callback__unique_email因为某些原因我发现codeigniter回调不像额外的差距这更好callback__uniqueemail

如果私有不起作用,则使公共功能删除下划线

public function uniqueemail() // no need $str

当公开时不要从这里删除额外的下划线callback_uniqueemail

echo Modules run的另一件事最好只从视图加载。

在您的控制器中,替换使用$this->load->view();

运行的echo模块

您需要在下面创建库后添加$this->form_validation->run($this) add $ this来运行。

并创建一个新库

<?php

class MY_Form_validation extends CI_Form_validation {

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

} 

教程最佳https://www.youtube.com/watch?v=8fy8E_C5_qQ