Welcome Guest, Not a member yet? Register   Sign In
Form validation callback in HMVC

I am developing an application using codeigniter HMVC architecture. I am pretty new to HMVC and have just started to explore it. Recently I began to transform my code into HMVC from MVC in codeigniter. I have developed a custom authentication library which resides in my application/libraries folder which has authentication rules for registration and login form. I have written some callback functions in my validations which suddenly stopped working when I opted for HMVC framework. I came across some references to fix this but all in vain. I couldn't somehow make my code working. According to a common suggestion I have built up a MY_Form_validation as below:

class MY_Form_validation extends CI_Form_validation
    public $CI;

And then in my authentication library I am using this:
$this->CI = & get_instance();
                $this->CI->form_validation->CI = $this->CI;
and for validation I use this:
But somehow my validation check is not being performed. It used to work on my MVC framework.

class MY_Form_validation extends CI_Form_validation {
  public $CI;

and the controller:
class Some_controller extends CI_Controller {
  function some_function_with_form_validation()
    $this->form_validation->CI =& $this;

All above according to


But I am using a custom library where I have set the rules and messages for authentication of the form for which I use
$this->CI = &get;_instance();?>
. When I call the functions in the library they automatically perform appropriate validations. What you are suggesting is performing validations using a controller

I haven't worked enough with HMVC to say for sure, but...

1) You don't need to define $CI / use get_instance() again when extending the Form Validation library - it's already there in the base class.

2) Ideally, if you're going to extend the library anyway, just turn your callbacks into additional rules in the library. Then you don't need to worry about the controller scope or reassigning the CI object.

@Aken: You're right but also false. The assign of $CI is indeed in CI_Form_validation, but it won't work with HMVC (as long as one is using the MX_Controller class since this does not extend the CI_Controller)
And therefore you need to assign your active controller (in other words the CI super object) back to the $CI property of CI_Form_validation (or MY_Form_validation, doesn't matter for this case)

Anyhow, I'm not able to determine where the problem stems from... Not yet

That's what I get for posting about HMVC when I don't use it, lol.

It seems like such an awful run-around to retain CI functionality. Maybe I'll look into it and see if I can help improve it.

Didn't want to criticize you, Aken.
As far as I understand the problem, once part (to assign $this to $this->form_validation->CI) comes from the widgetized (hierachical) MVC. If you don't need to run controllers from other modules within your current module, you're fine just using CI_Controller (instead of MX_Controller).

However, since CI 2.0.1 I think the $CI property of CI_Form_validation is private and no longer public. Since you want to change it with the fact stated above you need to change it from private to public so it can be accessed from out of the library.

It is indeed one part that needs to be optimized, but I'd wait for CI 3.0 or the development of the hmvc-lite branch on Codeigniter's GitHub. Maybe it will solve this problem and implement an awesome HMVC logic Wink

No offense taken - I don't mind people telling me if I'm wrong.

@sanks, Extending the CI Form Validation library is a good option when requiring reusable validations in your HMVC application.

Nothing special is required to make it work with Modular Extensions - HMVC, it will function the same as if it was extended in legacy CI.

The methods you have added to the MY_Form_validation class are not 'callbacks' they will function as core validation methods.

Theme © iAndrew 2016 - Forum software by © MyBB