![]() |
Use a set_rules callback in a libraries - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Use a set_rules callback in a libraries (/showthread.php?tid=72263) |
Use a set_rules callback in a libraries - xenos92 - 11-28-2018 Hello, I want to use a custom function to control an image field. It works if I use it in the same controller with a callback ... Exemple in my Administration Controller : PHP Code: if($config_rules['images'] == true){ $this->form_validation->set_rules('images','images','callback_require_upload');} It works. The problem is that I want to put my validation rule in a Form_validation libraries like this: PHP Code: class MY_Form_validation extends CI_Form_validation{ PHP Code: if($config_rules['images'] == true){ $this->form_validation->set_rules('images','images','require_upload');} But I don't understand why it not works ?! RE: Use a set_rules callback in a libraries - jreklund - 11-28-2018 I don't have the time to write and give you a complete answer. But what you are looking for are this: https://www.codeigniter.com/userguide3/libraries/form_validation.html#callable-use-anything-as-a-rule This example uses users_model. So you will have to do similar. RE: Use a set_rules callback in a libraries - InsiteFX - 11-28-2018 @jreklund, Is correct you can only have a callback in the controller or in the model like he stated. I put my callbacks in the controller and then link them into my libraries. controller/callback -> to/method in library RE: Use a set_rules callback in a libraries - dave friend - 11-28-2018 You can extend CI_Form_validation and define new callbacks there. Some notes about your implementation of MY_Form_validation. You do not need to add the property $CI. The parent class CI_Form_validation already has that property and it is accessible in the extending class. You don't need MY_Form_validation::__construct because it does not do anything besides call parent:: __construct. PHP will call the parent constructor without your help. In the new callback you don't have to find the instance of form_validation because you're in the instance you're looking for. All that said, your class should look like this. PHP Code: class MY_Form_validation extends CI_Form_validation As you stated, it would be used like this PHP Code: if($config_rules['images'] == true){ $this->form_validation->set_rules('images','images','require_upload');} Not sure why you have the callback always returning false. Maybe you're not done writing it yet? RE: Use a set_rules callback in a libraries - dave friend - 11-28-2018 (11-28-2018, 08:15 AM)xenos92 Wrote: Exemple in my Administration Controller : If that code above is a controller you don't need $CI. Use $this instead. The function get_instance() returns a reference to the controller that is running and that's where you already are. Not that it matters if you extend CI_Form_validation as I suggested. But keep it in mind for future use. |