Search code examples
codeignitercallbackvalidationpyrocms

PyroCMS - form_validation e callback


I have a custom function to validate a field, but when i test it I have this error from PyroCMS:

Fatal error: Uncaught exception 'Exception' with message 'Undefined callback "$rule" in admin_razze' in /Applications/MAMP/htdocs/pyrocms/system/cms/libraries/MY_Form_validation.php:178 Stack trace: #0 /Applications/MAMP/htdocs/pyrocms/system/codeigniter/libraries/Form_validation.php(341): MY_Form_validation->_execute(Array, Array, 'test') #1 /Applications/MAMP/htdocs/pyrocms/addons/shared_addons/modules/canile/controllers/admin_razze.php(46): CI_Form_validation->run() #2 [internal function]: Admin_razze->nuovo() #3 /Applications/MAMP/htdocs/pyrocms/system/codeigniter/core/CodeIgniter.php(352): call_user_func_array(Array, Array) #4 /Applications/MAMP/htdocs/pyrocms/index.php(280): require_once('/Applications/M...') #5 {main} thrown in /Applications/MAMP/htdocs/pyrocms/system/cms/libraries/MY_Form_validation.php on line 178

this is my code but I can't see the problem...if I remove the callback it work well

function nuovo(){

        //Carico librerie e helper per la gestione del form
        $this->load->helper('form');
        $this->load->library('form_validation');

        //Imposto le regole di validazione
        $this->form_validation->set_rules('nome','Nome','trim|required|callback_controllo_esistenza');
        $this->form_validation->set_rules('copertina','Immagine di copertina','trim|required|xss_clean');

        if($this->form_validation->run() == FALSE){
            $this->template->build('admin/razze/inserimento');          
        }
        else{

            //Recupero i campi e creo l'array per l'inserimento
            $dati_insert = array(
                'nome' => ucfirst($this->input->post('nome')),
                'copertina' => $this->input->post('copertina')
            );

            //Carico il modello per salvare la razza
            $this->load->model('gestione','',TRUE);

            //Invio i dati al model
            $this->gestione->salva_razza($dati_insert);


            redirect('admin/canile/razze');
        }

    }

    public function callback_controllo_esistenza($str){
        if($str=="test"){

            return FALSE;
        }

    }

Solution

  • I think your function should be named like the standard CI form validation callbacks, IE,

    public function controllo_esistenza($str)
    {
       if($str=="test")
       {
         return FALSE;
       }
    }
    

    Although I would do this:

    $this->form_validation->set_rules('nome','Nome','trim|required|callback__controllo_esistenza');
    

    And then this:

    public function _controllo_esistenza($str)
    {
       if($str=="test")
       {
         return FALSE;
       }
    }
    

    So your callbacks don't somehow get called via the request.