Moving validation to models |
[eluser]JonoB[/eluser]
CI validation currently runs in the controller. Is it possible to move this into Models? My main rationale is that in addition to my web front end, I would like to use the same models for a Rest API. I am aware that the DataMapper ORM allows for model validation. Are there any other non-orm solutions that allow this?
[eluser]Isern Palaus[/eluser]
Take a look to the base model by Jamie Rumbelow and the _run_validation() function by Dan Horrigan: https://bitbucket.org/jamierumbelow/code...php#cl-517 I think it is very descriptive. Wish it helps! ;-)
[eluser]JonoB[/eluser]
[quote author="Isern Palaus" date="1300683671"]Take a look to the base model by Jamie Rumbelow and the _run_validation() function by Dan Horrigan: https://bitbucket.org/jamierumbelow/code...php#cl-517 I think it is very descriptive. Wish it helps! ;-)[/quote] That looks like it will do the trick, thank you!
[eluser]JonoB[/eluser]
I have now implemented validation in my models based on the link above, and using a MY_Model extends CI_Model methodology. For the most part, this works well. However, the one problem that I have not been able to solve is that my custom callbacks do not work. I eventually tracked this down to line 588 in the Form_validation library - basically the method_exists function does not return true. Code: if ( ! method_exists($this->CI, $rule)) I dumped the $rule param to double check that the name of the callback function was correct. I am missing something obvious here, but not quite sure what. Relevant excerpt from my model is below. Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
[eluser]Madmartigan1[/eluser]
[quote author="JonoB" date="1300672592"]CI validation currently runs in the controller.[/quote] CI Validation runs wherever you tell it to. You can run it in your view if you really want to :-S @callbacks: The form validation class uses the $CI variable internally to do callbacks, which is assigned to the current controller. Try the function below and pass the object you want to do callbacks to the first parameter. Code: // Like this: $this->form_validation->run($my_callback_object); * Thank you wiredesignz
[eluser]JonoB[/eluser]
Yep, thanks for the reply. I eventually figured out that callbacks, by default, only look in the controller. So, I extended Form_validation along the lines of http://codeigniter.com/wiki/MY_Form_vali...to_Models/ Callbacks now work properly! Next step is that prepping doesnt work at all for some reason...working on that now. |
Welcome Guest, Not a member yet? Register Sign In |