扩展表单验证类并在codeigniter中添加自定义验证规则

What I'm trying to do is to extend the form validation class and add some custom validation rules there, but for some reasons codeigniter can't see any of them...

I have created new file inside libraries folder called MY_Form_validation.php and added the following code:

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

class MY_Form_validation extends CI_Form_validation {

    function valid_date($str)
    {
        return FALSE;
    }

}

But the validation_errors() function never returns any errors (I have stored the error message in config folder), if I place valid_date function inside a controller it works fine. Any ideas?

I don't know exactly what the answer is but I've got this possibility in my mind, are you autoloading my_form_validation instead of form_validation? because if you load form_validation, there might be a chance that your CI instance isn't aware of your class?

You need to set a message.

if(condition)
{
  return true;
}
else
{
   $this->CI->form_validation->set_message('function_name', 'message');
   return false;
}

How about changing your MY_Form_validation.php code, so it has __construct() function and calls the parents __construct(), like so:

class MY_Form_validation extends CI_Form_validation {

    public function __construct()
    {
        parent::__construct();
    }

    public function valid_date($str)
    {
        return FALSE;
    }

}