简体   繁体   English

无法找到自定义回调验证规则(CodeIgniter 3 HMVC)

[英]Unable to find custom callback validation rule (CodeIgniter 3 HMVC)

EDIT2: Scroll down for most up-to-date information! EDIT2:向下滚动以获取最新信息!

In CodeIgniter 3, I recently moved callback rules across all of my controllers to application/libraries/MY_Form_validation.php to prevent code repetition etc., and to clean up. 在CodeIgniter 3中,我最近将所有控制器上的回调规则移到了application/libraries/MY_Form_validation.php以防止代码重复等,并进行清理。

Right now, they don't seem to be working anymore. 目前,他们似乎不再工作了。

MY_Form_validation.php starts like this: MY_Form_validation.php像这样启动:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation
{
    public $CI;

    function run($module = '', $group = '')
    {
        log_message('debug',"Now running the MY_Form_validation");
        (is_object($module)) AND $this->CI = &$module;
        return parent::run($group);
    }

Then a whole list of callback function follows, all defined as public function callback_name(){} . 然后是整个回调函数列表,全部定义为public function callback_name(){}

I also have one (in the same class) which checks if the provided user login information is correct (and thus, if the user can login etc.), but the form validation can't find the rule. 我也有一个(在同一个类中),它检查所提供的用户登录信息是否正确(因此,用户是否可以登录等),但是表单验证找不到规则。

The error log looks like this: 错误日志如下所示:

INFO - 2016-06-23 13:33:18 --> Form Validation Class Initialized
DEBUG - 2016-06-23 13:33:18 --> Now running the MY_Form_validation
INFO - 2016-06-23 13:33:18 --> Language file loaded: language/english/form_validation_lang.php
DEBUG - 2016-06-23 13:33:18 --> Unable to find callback validation rule: check_database

The first DEBUG message indicates that MY_Form_validation is loaded (as its overwritten run() method is used, hence the debug logging), but it somehow can't find the callback functions clearly defined below. 第一个DEBUG消息指示已加载MY_Form_validation (因为使用了其覆盖的run()方法,因此使用了调试日志记录),但它却无法以某种方式找到下面明确定义的回调函数。

I also included language file application/language/english/form_validation_lang.php with the following line: 我还在以下行中包含了语言文件application/language/english/form_validation_lang.php

$lang['form_validation_check_database'] = 'The password does not match the username. Try again.'; , which it catches correctly (ie this message is displayed when performing the form validation), but it somehow cannot find the callback function itself. ,它可以正确捕获(即,在执行表单验证时会显示此消息),但是无法以某种方式找到回调函数本身。

EDIT: I checked to see if methods weren't inherited correctly: 编辑:我检查以确定方法是否未正确继承:

    public function __construct($rules = array())
    {
    $this->CI =& get_instance();
    var_dump(get_class_methods($this));

The var_dump() does output the correct, full array of methods, both my own custom callbacks and the built-in ones. var_dump()确实输出正确的完整方法数组,包括我自己的自定义回调和内置的回调。

EDIT2: I read the system/libraries/Form_validation.php and investigated where the debug message occurs, which can be seen in this code sample (line 734-749) : EDIT2:我阅读了system/libraries/Form_validation.php并调查了调试消息发生的位置,可以在此代码示例中看到(行734-749):

    // Call the function that corresponds to the rule
    if ($callback OR $callable !== FALSE)
    {
        if ($callback)
        {
            if ( ! method_exists($this->CI, $rule))
            {
                log_message('debug', 'Unable to find callback validation rule: '.$rule);
                $result = FALSE;
            }
            else
            {
                // Run the function and grab the result
                $result = $this->CI->$rule($postdata, $param);

            }
        }

It seems as though callbacks are only looked for in the main CI object, but not in the form validation library itself. 似乎只在主CI对象中查找了回调,而在表单验证库本身中却没有。 I could add some hacky exceptions that would pick the library callbacks, but I doubt that that's the best thing to do and I guess I'm overlooking something simple... 我可以添加一些hacky异常来选择库回调,但是我怀疑那是最好的做法,而且我想我忽略了一些简单的事情……

If any additional info is required, please let me know. 如果需要任何其他信息,请告诉我。

Alright, I found out... 好吧,我发现了...

Apparently, as soon as you move callback functions to MY_Form_validation.php, they are actually built-in validation rules, and no longer act as callbacks. 显然,将回调函数移至MY_Form_validation.php后,它们实际上是内置的验证规则,不再充当回调函数。

When setting form rules, I still had the callback_ prefix applied, which makes the Form_validation library look for the rule in the normal CI object (ie in the controller) rather than the (MY_)Form_validation class. 设置表单规则时,我仍然应用了callback_前缀,这使Form_validation库在普通CI对象(即在控制器中)而不是(MY_)Form_validation类中查找规则。

The fix for me was to simply remove this prefix from the applied validation rule settings. 对我来说,解决方法是简单地从应用的验证规则设置中删除该前缀。 Now it finds the 'callback' functions correctly. 现在,它可以正确找到“回调”功能。

Please Try This 请尝试一下

<?php

class Form extends CI_Controller {

        public function index()
        {
                $this->load->helper(array('form', 'url'));

                $this->load->library('form_validation');

                $this->form_validation->set_rules('username', 'Username', 'callback_username_check');
                $this->form_validation->set_rules('password', 'Password', 'required');
                $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
                $this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]');

                if ($this->form_validation->run() == FALSE)
                {
                        $this->load->view('myform');
                }
                else
                {
                        $this->load->view('formsuccess');
                }
        }

        public function username_check($str)
        {
                if ($str == 'test')
                {
                        $this->form_validation->set_message('username_check', 'The {field} field can not be the word "test"');
                        return FALSE;
                }
                else
                {
                        return TRUE;
                }
        }

}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM