Codeigniter & HMVC - Callback Not Working
I've gone through quite a bit of the links and solutions provided here and elsewhere, but I just can't seem to solve the callback problem I am facing. I am using Codeigniter with HMVC, below code.
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);
}
It would be a great help if someone could point me in the right direction to solve the problem. thanks in advance
Naveen
source to share
The first of you is missing from the rules
$rules['email'] = 'required|callback__uniqueemail';
Also the callback function should be different callback__unique_email
for some reason i found that calling callignign doesn't look like an extra span, it's bettercallback__uniqueemail
If private ones don't work, make a public function by removing the underscore
public function uniqueemail() // no need $str
When you post information about not removing the extra underscore here callback_uniqueemail
Another thing is what is echo Modules run
best to load from a view.
In your controller, replace the echo modules with $this->load->view();
You need to add $this->form_validation->run($this)
add $ this to run after creating the 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 https://www.youtube.com/watch?v=8fy8E_C5_qQ
source to share
According to wiredesignz,
When using form validation with MX, you need to extend the CI_Form_validation class as shown below,
/** application/libraries/MY_Form_validation **/
class MY_Form_validation extends CI_Form_validation
{
public $CI;
}
before assigning the current controller as the $ CI variable for the form_validation library. This will allow your callback methods to function as expected.
class Xyz extends MX_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->form_validation->CI =& $this;
}
}
This will fix the HMVC related callback issue without making any changes to your code.
source to share