Many of us are using the HMVC extension (Hierarchical Model View Controller) in codeigniter which is available. http://codeigniter.com/wiki/Modular_Extensions_-_HMVC
HMVC helps developing modular apps in a very convenient way. All the things work simply fine just like a clean CI installation. There’re 1 or two exceptions which i have found. One is the callback of Form_validation class. If we are making some forms like registration we check emails and usernames and we do it in the form validation class using callbacks. If we look at the basic callback structure when validating forms from CI documentation, it appears like.
< ?php
class Form extends Controller
{
function index()
{
$this->load->helper( array (
'form',
'url'
) );
$this->load->library( 'form_validation' );
$this->form_validation->set_rules( 'username', 'Username', 'callback_username_check' );
$this->form_validation->set_rules( 'password', 'Password', 'required' );
$this->form_validation->set_rules( 'passconf', 'Password Confirmation', 'required' );
$this->form_validation->set_rules( 'email', 'Email', 'required' );
if ($this->form_validation->run() == FALSE)
{
$this->load->view( 'myform' );
}
else
{
$this->load->view( 'formsuccess' );
}
}
function username_check($str)
{
if ($str == 'test')
{
$this->form_validation->set_message( 'username_check', 'The %s field can not be the word "test"' );
return FALSE;
}
else
{
return TRUE;
}
}
}
?>
But this callback will not simply work. Some of my precious hours went why it was not working. Then i navigated the Form_validation.php in /system/libraries folder around line 580 which looks like
if ($callback === TRUE)
{
if ( ! method_exists($this->CI, $rule))
{
continue;
}
The function – method_exists returns false even if the callback function is there in the Controller and after all in the CI loader object. So it came out from the forum that you have to do a little extension to Form Validation class which is
< ?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation
{
function run($module = '', $group = '') {
(is_object($module)) AND $this->CI =& $module;
return parent::run($group);
}
}
/* End of file MY_Form_validation.php */
/* Location: ./application/libraries/MY_Form_validation.php */
And when running the validation like in earlier code
if ($this->form_validation->run() == FALSE)
you have to provide an addition $this as a parameter to run method. which will look like
if ($this->form_validation->run($this) == FALSE)
Now, you validation callbacks will work perfectly.
Another note, if you’re trying to access a public variable of a controller via the get_instance() in hooks , you’ll have to write in your controller like this.
CI::instance()->var_name = “Some Value”;
May be there are some other required adjustments when using HMVC, but i came across this two time killing adjustments.
8 Responses to Form Validation Callbacks in HMVC in Codeigniter
Lupida
September 16th, 2009 at 6:16 am
Thanks for this post! It saved my day!
Steven
September 30th, 2009 at 8:43 pm
Thanks man, this works fine. I also spent precious hours of my time.
Altaf Hussain
October 8th, 2009 at 4:23 pm
Thanks man, i like this. I have spent hours to find out the problem, but i cant. I will try this method.
Thanks again for this.
Aizen
November 5th, 2009 at 10:50 pm
Thanks for this post! It saved my day! too.
greetings
Patrick
December 1st, 2009 at 6:45 am
Hey, this is a really good post. Not only it explain how to do, it also explains what to do. I really appreciate what you have done. It just incredibly save my day as well. Thank you much.
Dinesh Shah
January 29th, 2010 at 2:35 am
Wow!
I was just about to give up and landed on this blog!
Resolved the issue in no time! Great work.
I added your extension to CI HMVC (ME) it started working as expected!
Thanks is not enough!
Mahbub
January 29th, 2010 at 11:57 pm
I’m glad that my effort save some time. I just didn’t want people to waste hrs on a stupid problem like that.
ilumos
February 28th, 2010 at 9:45 pm
Oh man, so glad I found your blog. Was beginning to pull my hair out…
Don’t know why they haven’t incorporated this change into the newer versions of the HMVC extension.
Many many thanks!