2010-06-15 80 views
0

我正在使用将codeigniter表单验证规则保存到指定here的配置文件的技术,但似乎无法使其工作。Codeigniter表单验证配置文件不起作用

我也试图从我通过autoload.php机制自动加载的Validation_Callbacks.php库调用验证回调函数。

下面是我的form_validation.php表单验证规则配置文件中的一个片段:

<?php 
/* This config file contains the form validation sets used by the application */ 

$config = array(
    'register_user' => array(
      array(
        'field' => 'dob', 
        'label' => 'lang:register_dob', 
        'rules' => 'trim|required|date_check|age_check|xss_clean' 
       ), ... 
      ) 
     ) 

这里是Validation_Callbacks.php文件,即在应用程序/库生活:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 
/** 
* Custom validator library containing app-specific validation functions 
*/ 
class Validation_Callbacks { 
/* Checks that the given date string is formatted as expected */ 
function date_check($date) { 
    $ddmmyyyy='(0[1-9]|[12][0-9]|3[01])[- \/.](0[1-9]|1[012])[- \/.](19|20)[0-9]{2}'; 
    if(preg_match("/$ddmmyyyy$/", $date)) { 
     return TRUE; 
    } else { 
     $this->form_validation->set_message('date_check', $this->lang->line('error_register_dob_format')); 
     return FALSE; 
    } 
} 

/* Checks that the given birthday belongs to someone older than 18 years old. We assume 
* that the date_check method has already been run, and that the given date is in the 
* expected format of dd/mm/yyyy */ 
function age_check($date) { 
    $piecesDate = explode('/', $date); 
    $piecesNow = array(date("d"), date("m"), date("Y")); 
    $jdDate = gregoriantojd($piecesDate[1], $piecesDate[0], $piecesDate[2]); 
    $jdNow = gregoriantojd($piecesNow[1], $piecesNow[0], $piecesNow[2]); 

    $dayDiff = $jdNow - $jdDate; 

    if ($dayDiff >= 6570) { 
     return TRUE; 
    } else { 
     $this->form_validation->set_message('age_check', $this->lang->line('error_register_age_check')); 
     return FALSE; 
    } 
} 

} 

我使用标准调用此方法:

if ($this->form_validation->run('register_user') == FALSE) { 

我的回调函数ar没有被称为。有什么我在这里失踪?预先感谢您提供的任何帮助!

回答

1

确保:

当规则组被相同地命名 到控制器类/函数时自动运行() 函数是从 类/函数调用将 被使用。

要测试你可以尝试使用:

$this->load->config('configname'); 
+0

感谢基兰,我给他们任意名称,然后尝试使用$这 - > form_validation->运行(“名”)。这应该是按照文档工作的,但是你的解决方案更好 - 它阻止了将验证调用用于委托方法,迫使我将它包含在顶层“控制器/函数”函数中。 – ubermensch 2010-06-18 14:27:52