Codeigniter 表单验证:在将输入发送到回调函数之前验证输入

Codeigniter form validation: validate input before sending it to callback function

我使用 Codeigniter 回调函数(请参阅下面的代码),我想知道它是否在将输入发送到回调函数之前检查输入是否为 alpha_numeric,因为我不知道想将非字母数字输入传递给我的回调函数。

$this->form_validation->set_rules('code', 'Code', 'alpha_numeric|callback_check_code');

    public function check_code($value) {        

    $this->model_abc->did_check_code($value);
    $this->form_validation->set_message('check_code', 'Please enter the code correctly.');
        }

更新:

我发现在 Codeigniter 中规则是 运行 从左到右,一旦失败,它就会停止检查并将该字段标记为 'not passed validation' 并将消息设置为第一个失败的规则.因此,如果输入不是字母数字,则不会传递给我的回调函数。

如果您想为现有库编写自己的规则,请执行以下步骤:

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


class MY_Form_validation extends CI_Form_validation {

    function __construct($rules = array()) {
        parent::__construct($rules);  
        log_message('debug', '*** Hello from MY_Form_validation ***');
    }


    function check_code($postcode) {
        // Do it according to your need
    }
}

并将文件保存在 application/libraries 中。有关更多信息,请查看以下 link https://ellislab.com/codeigniter/user-guide/general/creating_libraries.html 并查看 Extending Native Libraries

部分

更新:

首先了解规则是如何工作的'alpha_numeric|callback_check_code'验证库逐一检查给定的规则,并根据错误或值进行设置。意味着如果 alpha_numericcallback_check_code 都验证了数据那么它 return $this->form_validate->run() == true.