Welcome Guest, Not a member yet? Register   Sign In
Problem iwith validation callback
#1

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;
 
               }
 
       
Reply
#2

Try this:

PHP Code:
if (($annee <= '1870') || ($annee >= $current_year)) 
What did you Try? What did you Get? What did you Expect?

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

(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;
                        
 
       
Reply
#4

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




Theme © iAndrew 2016 - Forum software by © MyBB