【问题标题】:Codeigniter form validation config file not workingCodeigniter 表单验证配置文件不起作用
【发布时间】:2010-06-15 20:42:26
【问题描述】:

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

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

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

<?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) {

我的回调函数没有被调用。我在这里缺少什么吗?提前感谢您提供的任何帮助!

【问题讨论】:

    标签: codeigniter validation callback config


    【解决方案1】:

    确保:

    当规则组名称相同时 到控制器类/功能,它将 在 run() 时自动使用 函数从中调用 类/函数。

    要测试,您可以尝试使用:

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

    【讨论】:

    • 感谢 Kieran,我给了他们任意名称,然后尝试使用 $this->form_validation->run('name')。这应该根据文档工作,但您的解决方案更好 - 它只是阻止我将验证调用转移到委托方法,并迫使我将其包含在顶级“控制器/函数”函数中。跨度>
    猜你喜欢
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 2020-09-28
    • 2012-02-26
    相关资源
    最近更新 更多