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沒有被稱爲。有什麼我在這裏失蹤?預先感謝您提供的任何幫助!
感謝基蘭,我給他們任意名稱,然後嘗試使用$這 - > form_validation->運行(「名」)。這應該是按照文檔工作的,但是你的解決方案更好 - 它阻止了將驗證調用用於委託方法,迫使我將它包含在頂層「控制器/函數」函數中。 – ubermensch 2010-06-18 14:27:52