CodeIgniter Forums
Problem iwith validation callback - 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: Problem iwith validation callback (/showthread.php?tid=65095)



Problem iwith validation callback - nunenthal - 04-28-2016

Hello,

I write this bellow, my validation rules arre working, but the callback function not.
I'm new in codeigniter and maybe a little stupid, please help.



PHP Code:
public function add_annonce_do()
 
   {
 
       $this->form_validation->set_rules('modele''Modèle''required');
 
       $this->form_validation->set_rules('annee''Année''required|callback_annee_check');
 
       $this->form_validation->set_rules('km''km''required');
 
       $this->form_validation->set_rules('prix''Prix''required');
 
       $this->form_validation->set_rules('description''Description''required');
 
       if ($this->form_validation->run() == FALSE)
 
       {
 
           $this->load->view('admin_header');
 
           $data['marque']=$this->marque->get_all_marque();
 
           $data['carburant']=$this->carburant->get_all_carburant();
 
           $this->load->view('annonce_add_display',$data);
 
           $this->load->view('admin_footer');
 
       }
 
       else
        
{
 
           //traitement du formulaire
 
           $this->load->view('formsuccess');
 
       }
 
       
        

    
}
 
   //callback validation
 
   public function annee_check($annee)
 
       {
 
               $current_year=date('Y');
 
               if (($annee <= 1870) || ($annee >= $current_year))
 
               {
 
                       $this->form_validation->set_message('annee_check''L’année n’est pas valide');
 
                       return FALSE;
 
               }
 
               else
                
{
 
                       return TRUE;
 
               }
 
       



RE: Problem iwith validation callback - InsiteFX - 04-28-2016

Try this:

PHP Code:
if (($annee <= '1870') || ($annee >= $current_year)) 



RE: Problem iwith validation callback - nunenthal - 04-28-2016

(04-28-2016, 11:02 AM)InsiteFX Wrote: Try this:

PHP Code:
if (($annee <= '1870') || ($annee >= $current_year)) 

The problem is not that the callback function is not working, the problem is that it seem that the callback function is not called.

Even if I write this, the validation is TRUE


PHP Code:
public function annee_check($annee)
 
       {
                
 
                       $this->form_validation->set_message('annee_check''L’année n’est pas valide');
 
                       return FALSE;
                        
 
       



RE: Problem iwith validation callback - nunenthal - 04-29-2016

Solved,

In fact this is not possible :
$this->form_validation->set_rules('annee', 'Année', 'required|callback_annee_check');

You must write this
$this->form_validation->set_rules('annee', 'Année', 'callback_annee_check');
And create the error message manually in the callback function in case of empty field.