我正在使用将codeigniter表单验证规则保存到指定here的配置文件的技术,但似乎无法使其正常工作。
我也试图从Validation_Callbacks.php库中调用验证回调函数,我通过autoload.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) {
我的回调函数未被调用。这里有什么我想念的吗?提前感谢您提供的任何帮助!
答案 0 :(得分:1)
确保:
当规则组的名称相同时 它将控制器类/功能 运行时自动使用() 从中调用函数 类/函数。
要测试您可以尝试使用:
$this->load->config('configname');