CodeIgniter Forums
Form validation callback in HMVC - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Form validation callback in HMVC (/thread-53324.html)



Form validation callback in HMVC - El Forum - 07-19-2012

[eluser]sanks[/eluser]
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:

Code:
class MY_Form_validation extends CI_Form_validation
{
    public $CI;
}

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


Form validation callback in HMVC - El Forum - 07-19-2012

[eluser]PhilTem[/eluser]
library:
Code:
class MY_Form_validation extends CI_Form_validation {
  public $CI;
}

and the controller:
Code:
class Some_controller extends CI_Controller {
  function some_function_with_form_validation()
  {
    $this->load->library('form_validation');
    $this->form_validation->CI =& $this;
    
    $this->form_validation->run();
  }
}

All above according to

https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home


Form validation callback in HMVC - El Forum - 07-19-2012

[eluser]sanks[/eluser]
But I am using a custom library where I have set the rules and messages for authentication of the form for which I use
Code:
$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


Form validation callback in HMVC - El Forum - 07-19-2012

[eluser]Aken[/eluser]
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.


Form validation callback in HMVC - El Forum - 07-19-2012

[eluser]PhilTem[/eluser]
@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


Form validation callback in HMVC - El Forum - 07-19-2012

[eluser]Aken[/eluser]
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.


Form validation callback in HMVC - El Forum - 07-19-2012

[eluser]PhilTem[/eluser]
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


Form validation callback in HMVC - El Forum - 07-19-2012

[eluser]Aken[/eluser]
No offense taken - I don't mind people telling me if I'm wrong.


Form validation callback in HMVC - El Forum - 07-19-2012

[eluser]wiredesignz[/eluser]
@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.

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