Welcome Guest, Not a member yet? Register   Sign In
Use a set_rules callback in a libraries
#1

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');}

function 
require_upload(){ 

     
      $CI $this->CI =& get_instance(); 
     
      //$CI->load->model('question_model');

     
      $CI->form_validation->set_message('require_upload'"Vous devez uploader une image");
     
   return false;

    } 

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

    protected 
$CI  
     
    function __construct
($config = array()){
 
         parent::__construct($config);
 
    }
        

    function 
require_upload(){ 
        
     
      $CI $this->CI =& get_instance(); 

     
      $CI->form_validation->set_message('require_upload'"Vous devez uploader une image");
     
   return false;

        
    }

 And I call now this rules like this : 


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 ?!
Reply
#2

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/l...-as-a-rule

This example uses users_model. So you will have to do similar.
Reply
#3

@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
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

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
{
 
   function require_upload()
 
   {
 
       $this->set_message('require_upload'"Vous devez uploader une image");
 
       return false;
 
   }


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?
Reply
#5

(11-28-2018, 08:15 AM)xenos92 Wrote: Exemple in my Administration Controller :

PHP Code:
function require_upload(){ 

 
  $CI $this->CI =& get_instance(); 
 
  //$CI->load->model('question_model');

 
  $CI->form_validation->set_message('require_upload'"Vous devez uploader une image");
 
   return false;

 } 

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB