PyroCMS - form_validation e回调

时间:2012-02-28 15:40:25

标签: codeigniter callback validation pyrocms

我有一个自定义函数来验证字段,但是当我测试它时,我从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

这是我的代码,但我看不出问题...如果我删除回调它运作良好

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

    }

1 个答案:

答案 0 :(得分:1)

我认为您的函数应该像标准CI表单验证回调一样命名,IE,

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

虽然我会这样做:

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

然后这个:

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

所以你的回调不会以某种方式通过请求被调用。