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.
42 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!
Matías
May 11th, 2010 at 8:11 pm
From Argentina, i love you!!!!!!!
ben
May 27th, 2010 at 8:54 pm
THANK YOU!
izainurie
June 4th, 2010 at 10:18 am
i’ve tried to extend my form_validation and put $this into bracket after run.. i’ve test it on my localhost… it seems work perfect.. but when i transfered my files into my server.. validation callback not working….
can you help me ?
Khoa
July 7th, 2010 at 10:43 am
Thanks so much, I spent my time with this problem.
jazz
July 14th, 2010 at 8:34 am
Thanks your great job!It works!
Yanny
July 20th, 2010 at 8:14 pm
Thank you so much for pointing this out! I have spent many hours in this problem haha. =)
gebe
August 6th, 2010 at 5:12 am
Thanks a lot!!!!!!!!!!!!!!!!!!!!!
sharukh
August 28th, 2010 at 6:39 am
Thanks
Brett
September 21st, 2010 at 5:58 am
So if callbacks are broken, has it been fixed in the base CI yet?
Mahbub
September 21st, 2010 at 9:38 am
Not that I know as of yet. Actually HMVC is not part of the core CI distribution and hence Ellislab has no reason to be concerned why HMVC form validation callbacks are not working. If we’re taking the advantage and it has to be tweaked, it makes sense extend one of the classes to make it work.
Matthew
October 1st, 2010 at 11:10 pm
I’m so thankful for geniuses and pioneers like you. It would have taken me a long time to find that problem, thanks to you it only took about 10 min.
ReTsAm
October 3rd, 2010 at 6:36 am
is FINE! Good its really work .! thanks!
rifaioftheyear
January 24th, 2011 at 3:33 am
i still got error.. here is my code:
if ($this->form_validation->run($this) == FALSE){
$this->load->view(‘add_v’,$data);
}
else{
$this->crud_m->insertData();
}
when validation passed i’ve got error 404 on my page and doesn’t process my method insertData();
Mahbub
January 24th, 2011 at 6:49 am
There’s no way that you’ll get a 404 error when Validation has passed. There must be a problem with the crud_m (a custom crud library i believe ) library.
Why don’t you test this :
if ($this->form_validation->run($this) == FALSE){
$this->load->view(‘add_v’,$data);
}
else{
die(“Form validation Passed”);
//$this->crud_m->insertData();
}
If you get the output “Form validation Passed” on a blank page after validation passes, it means there’s a problem with the crud_m library. Moreover you can always use turn your error_threshold=4 in your application/config/config.php and watch the log for errors.
Hope that helps.
Aleks
January 29th, 2011 at 6:09 am
Thanks for this post. I’m first using HMVC and have this problem too. It really works!:)
Lorenzo
February 7th, 2011 at 6:18 pm
Hi!
I’ve solved callback issue with this guide but when i try to deploy the application on my production server i got this error:
Severity: Warning
Message: Illegal offset type in isset or empty
Filename: libraries/Form_validation.php
Line Number: 303
Please Help Me!!
Best Regards!
Mahbub
February 7th, 2011 at 9:30 pm
You probably have a problem passing the parameter as array. I can possibly comment more when i see more of your code.
Lorenzo
February 7th, 2011 at 11:02 pm
Thank you for the quick answer!!
I’m using Codeigniter 2.0 + MX + Doctrine
controller code:
____________________________________________________________
if($this->form_validation->run($this,’add-case’) == false):
$errors = (validation_errors() != ”) ? validation_errors() : ”;
else:
//do something
endif;
____________________________________________________________
the validation rules are in a separate file (form_validation.php in my module/config folder)
and contains a callback that not work (it return even TRUE).
Thank you!
Mahbub
February 8th, 2011 at 2:20 am
It looks like the validation rules are not being registered or there’s no validation rules at all. Can you try without the validation group ? Moreover, can you email me the full Controller code ? May be i can debug more.
Lorenzo
February 8th, 2011 at 3:11 am
I’ve put some log_message in method “run” of MY_Form_validation class but nothing!! It’s look that MY_Form_validation class is never been loaded.
deepika
April 1st, 2011 at 5:35 pm
good yar.. its realy help me.. thanx dod
Morteza Ebrahimi
April 3rd, 2011 at 2:51 pm
Thanks mahbub , I lost 4 hours to debug my code.You saved my day.
Regards
karthi
April 8th, 2011 at 11:43 am
HI mahbub,
Am using codeigniter 2.0.0…Am using your call back code..It is work well on localhost..But it is not work on the Server..Actually what happened the MY_Form_validation files are not executed on server..So callback doesn’t work…May you help me?
Mahbub
April 8th, 2011 at 5:47 pm
@Karthi : I’ve just checked CI version 2.0.2 and extended the Validation Library and it does get called.
Would be nice if you can email me the code [Delete every other files on the project and just one controller which is using the Validation], so that i can take a deeper look.
Karthi
May 14th, 2011 at 3:05 pm
@Mahbub : Thanks for your reply..Sry for my reply after long period to you…Am find problem on my file name …Your coding perfectly work ..Thanks **Mahbub** for this nice coding….
David
May 19th, 2011 at 4:27 am
This actually really works well – its a little confusing as to what is actually happening. It looks like you are changing $this->CI which is pointing to the controller to be the actual model and then the validation etc gets added to that as opposed to the controller. Is this correct?
Wasim Qamar
May 22nd, 2011 at 9:04 pm
Mahbubur Yaara kaam kr diya!
real good job! would like to coordinate with you more.
Please contact me on my email or provide your address.
Bart
July 21st, 2011 at 5:54 pm
Thx Mr! This saved my day;)
steve
August 9th, 2011 at 11:41 pm
Thanks for Sweden!! It worked perfectly!
Alma
August 10th, 2011 at 6:32 am
Works like a charm! I was about to cry after hours trying to figure this out when I found your neat solution. Thanks from Spain
Manuel
September 25th, 2011 at 10:39 pm
Thanks a lot from Venezuela…. It worked just perfect !!
ramon
November 12th, 2011 at 3:50 pm
thank you
Rafael
November 15th, 2011 at 10:14 pm
You don’t need to extend the Form library.
Just put this after you load the form_validation library
$this->form_validation->CI =& $this;
eg:
$this->load->library(‘form_validation’);
$this->form_validation->CI =& $this;
I know this is an old post, but there are some recent comments
suraj
February 1st, 2012 at 1:20 pm
thanks…………………..It saved my time!!