CodeIgniter Forums
Extend MY_Form_validation not work - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Extend MY_Form_validation not work (/thread-65732.html)



Extend MY_Form_validation not work - xenos92 - 07-17-2016

I'm locked for extend my form_validation rules...
In my controller I have a function article_new() who have this rules :


PHP Code:
$this->form_validation
    
->set_rules('titre','Titre','trim|required|xss_clean|encode_php_tags')
 
   ->set_rules('image','Image','callback_handle_upload')
 
   ->set_rules('tags','Tags','trim|required|xss_clean|callback_tags|encode_php_tags')
 
   ->set_rules('article','Article','trim|required|xss_clean|encode_php_tags'); 


Here my function handle_upload :


PHP Code:
   <?php
    function handle_upload
()
 
   {
 
       if (isset($_FILES['image']) && !empty($_FILES['image']['name']))
 
       {
 
           
        
}
 
       else
        
{
 
           // throw an error because nothing was uploaded
 
           $this->form_validation->set_message('handle_upload'"Vous devez uploader une image");
 
           return false;
 
       }
 
   }
 
   ?>


My function works well but I want to move this out of my controller.
So I put the function in the MY_Form_validation library located in application/librairies/MY_Form_validation.php.
If I do this, and I remove "callback_" in my rules it doesn't work... and I don't know why Sad
I work with the 2.2.6 version.


RE: Extend MY_Form_validation not work - Avenirer - 07-17-2016

You are not showing us your MY_Form_validation.php...


RE: Extend MY_Form_validation not work - xenos92 - 07-17-2016

Thanks for your reply !
I have that in MY_Form_validation file ( in application/libraries/ ) : 


PHP Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
 
class 
MY_Form_validation extends CI_Form_validation{

 
   function __construct()
 
   {
 
       parent::__construct();
 
    

   
    function handle_upload
($image,$param)
 
         
        if
(isset($_FILES['image']) && !empty($_FILES['image']['name']))
 
       {
 
           
        
}
 
       else
        
{
 
           // throw an error because nothing was uploaded
 
           $this->form_validation->set_message('handle_upload'"You must upload an image");
 
           return false;
 
       }
 
   }




RE: Extend MY_Form_validation not work - PaulD - 07-18-2016

In your rule set_rules('image','Image','callback_handle_upload') you need to remove the callback_ from the function call.

Hope that helps,

Paul.


RE: Extend MY_Form_validation not work - xenos92 - 07-18-2016

(07-18-2016, 07:32 AM)PaulD Wrote: In your rule set_rules('image','Image','callback_handle_upload') you need to remove the callback_ from the function call.

Hope that helps,

Paul.

Thanks for your answer, but I said in my first message that I remove the "callback_" in my rules when I put my function in MY_Form_validation file and it's why I don't understand what I missed ...


RE: Extend MY_Form_validation not work - Narf - 07-19-2016

1. STOP USING CI2!
2. Pay attention to context.

$this->form_validation inside doesn't exist, it needs to be just $this.