无法在codeigniter中调用MY_Form_validation中的自定义验证

时间:2018-04-26 07:49:22

标签: php codeigniter validation

我在代码点火器中创建了一个自定义验证类。

class MY_Form_validation extends CI_Form_validation {

    function __construct($rules = array()) {
        parent::__construct($rules);
    }

    public function required_fr($str) {
        var_dump('test');
        if (!is_array($str)) {
            return (trim($str) == '') ? FALSE : TRUE;
        } else {
            return (!empty($str));
        }
    }
}

我的控制器看起来像这样:

class Ticket extends CI_Controller { 

    function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="alert alert-danger alert-dismissable"><a aria-hidden="true" data-dismiss="alert" class="close" type="button">×</a>', '</div>');            
    }

    public function new_ticket() {
        $this->form_validation->set_rules('category', 'categorie', 'required_fr');
        if ($this->form_validation->run() == TRUE) {
            ...
        }
    }
}

如果我没有填写字段'category',则不会调用验证'required_fr'。我从未在屏幕上的var_dump中收到消息“test”。

如果我输入'required'而不是'required_fr',我会在页面中找到绑定到必需的正确消息。如果我把required_fr,我根本没有收到任何消息。

我也尝试在表单验证的set_rules函数中添加callback_,但这似乎也不起作用。

如果我将一个var_dump放在MY_Form_validation类的构造函数中,我会看到它弹出我的页面,因此自定义验证类会被加载。

我错过了什么?

2 个答案:

答案 0 :(得分:0)

类MY_Form_validation扩展CI_Form_validation

class MY_Form_validation extends CI_Form_validation{    
    function __construct($config = array()){
        parent::__construct($config);
        $this->CI =& get_instance();
        $this->_config_rules = $config;
    }   
    public function required_fr($str) {
        if (!is_array($str)) {
            if(!empty(trim($str))){ return true;
            }else{
                $this->CI->form_validation->set_message('required_fr', 'String value is empty');
                return false;
            }
        } else {
            if(array_filter($str){ return true;
            }else{
                $this->CI->form_validation->set_message('required_fr', 'Array value is empty');
                return false;
            }
        }
    }
}

类Ticket扩展了CI_Controller

class Ticket extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->library('form_validation');        
        $this->form_validation->set_error_delimiters('<div class="alert alert-danger alert-dismissable"><a aria-hidden="true" data-dismiss="alert" class="close" type="button">×</a>', '</div>');            
    }
    public function new_ticket() {
        $this->form_validation->set_rules('category', 'categorie', 'trim|required_fr');
        if ($this->form_validation->run() == TRUE){
            // success
            _e($this->input->post());
        }else{
            _e($this->form_validation->error_array());
        }
    }
}

答案 1 :(得分:0)

尝试代替$ str使用输入帖子

Filename application / libraries / MY_Form_validation.php

<?php

class MY_Form_validation extends CI_Form_validation {

protected $CI;

public function __construct() {
    $this->CI =& get_instance();
}

public function required_fr() {
    if (!is_array($this->CI->input->post('your_input_name'))) {

        if (trim($this->CI->input->post('your_input_name')) == '') {
            $this->set_message('required_fr','some message');
            return false;
        } else {
            return true;
        }
    }
}