Form Validation Callbacks in HMVC in Codeigniter

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.

 

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

 

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

 

And when running the validation like in earlier code

you have to provide an addition $this as a parameter to run method. which will look like

Now, your 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.

Author's gravatar

Md. Mahbubur Rahman

70 thoughts on “Form Validation Callbacks in HMVC in Codeigniter

    Author's gravatar

    Hi Mahbubur,
    From the comments I could see that ur hack seems to work for the majority. However I am facing the problem even after the fix is in place.
    I am having my library loading and validation rule as:
    ………………
    $this->load->library(array(‘form_validation’, ‘session’));
    …………………
    $this->form_validation->set_rules(‘captcha’, ‘captcha’, ‘required|callback_checkcaptcha’);

    I have checkcaptcha function in place and running the validation check as
    $this->form_validation->run($this) === FALSE

    I have placed MY_Form_validation.php in application/libraries folder.
    But still the callback does not seem to work.
    Could someone help me out? Thanks

    Author's gravatar

    Ok now it works fine. Made a stupidity of including the callback function within another function in the controller rather than keeping it just as a function within controller.

    Author's gravatar

    What about this:
    ——————————

    ci =& get_instance();
    }

    function run($group = ”) {
    $obj = new $this->ci->router->class();
    (is_object($obj)) AND $this->CI =& $obj;
    return parent::run($group);
    }

    }

    /* End of file MY_Form_validation.php */
    /* Location: ./application/libraries/MY_Form_validation.php */

    ————————————-
    Call run method as usual
    ————————————-

    if ($this->form_validation->run() == FALSE)

    Author's gravatar

    Thank you soooooo much for this. I was completely stumped as to why my callbacks where not working.

    Author's gravatar

    I use Code igniter 2.1.3. I follow your instruction. But it have problem, It is say :

    Parse error: syntax error, unexpected ‘;’, expecting T_PAAMAYIM_NEKUDOTAYIM in /home/asutarko/public_html/spiritcms/application/libraries/MY_Form_validation.php on line 6

    CI =& $module; <— it is error
    return parent::run($group);
    }
    }
    /* End of file MY_Form_validation.php */
    /* Location: ./application/libraries/MY_Form_validation.php */

    How I fix it?

    Author's gravatar

    I use codeigniter 2.1.3 too, and i always get problem.

    1. In my autoload file, i loaded the form_validation library. it permits me to don’t use $this->load->library() in my controller.
    2. I created MY_Form_validation.php file in application/libraries/

    I get errors like that:

    A PHP Error was encountered

    Severity: Warning

    Message: Cannot modify header information – headers already sent by (output started at C:\wamp\www\kdmobil\application\libraries\MY_Form_validation.php:8)

    How can i fix my problem?

    Author's gravatar

    I have created HMVC module in code igniter. Module name is “instutute” under this module i have created “instutute” controller.
    My problem is “http://localhost/projects/school/institute/” it will work but instead of of module “institute” i need to add one schoolname or username of one table like “http://localhost/projects/school/dayanand/” this time name is different but i need to access “institute” module with “institute” controller. May be we can do it code igniter routs or .htaccess file. Please any one have any idea regarding this concept. please send me one simple pack of HMVC module.

    Author's gravatar

    Thanks a lot you saved my day :)

    Author's gravatar

    Thanks a lot! It should however be noted that the ampersand (&) has been converted to & in the Form Validation extension code. That’s why Abdul (and I) got the “T_PAAMAYIM_NEKUDOTAYIM” error.

    Author's gravatar

    I’ve tried but did not succeed.
    the question:
    1. if we create a new file named “MY_Form_validation”? if so, in what folder stored in? I’m using CI 2.1.3
    2. whether the file (MY_Form_validation) in the load as we load another library?
    Can you explain in detail what needs to be done because I am still a beginner :)

    Author's gravatar

    Ok, all could go well. I was wrong, because before I save the file in the “system / libraries” should be in the “application / libraries” :)

    Author's gravatar

    Thank you so much, Mahbub, also from Switzerland!

    Author's gravatar

    It works great but now the form validations that i have in my form_validation config file who doesnt use callback dont work

    Author's gravatar

    Why I can not use $this->input->post(‘something’); in controller of something module? I tried several ways but It doesn’t work.

    Author's gravatar

    Thanks dude for this post , But not working for me , still the form_validation doesn’t call my call back

    Author's gravatar

    Thx Björn Andreasson as your solution. I’m sorry that I reply you too late..
    Yes, I don’t see word ‘&’. It should be removed.

    Author's gravatar

    Thank you so much.
    Cảm ơn rất nhiều.

Your email address will not be published. Required fields are marked *