扩展表单验证

时间:2015-05-08 12:49:16

标签: php codeigniter validation

我需要扩展Form Validation库,以创建一个返回错误数组的方法。

我用方法' get_error_array'创建了一个新库。在库文件夹中:

$autoload['libraries'] = array('form_validation', 'my_form_validation');

在autoload配置文件中,我有:

$this->my_form_validation->get_error_array()

但是当我在验证提交后在控制器中调用class MY_Form_validation extends CI_Form_validation { public function __construct(){ parent::__construct(); } public function get_error_array(){ return $this->_error_array; } } 时,该方法返回一个空数组。

注意:验证为FALSE。

有什么想法吗?

更新#1

根据文档,我更新了我的代码,但结果是一样的:

   $this->load->library('form_validation');
   /* Validation  */
   if($this->form_validation->run('join_request') === false){
      $e = $this->form_validation->get_error_array();
      var_dump($e);
   }

我禁用了自动加载,并从控制器手动加载库:

get_error_array

http://www.codeigniter.com/user_guide/general/creating_libraries.html

更新#2

我对构造函数和工作进行了回音。在# echo extending CI_Form_validation ... object(MY_Form_validation)[17] protected 'CI' => & ... protected '_field_data' => array (size=0) empty protected '_config_rules' => array (size=0) empty protected '_error_array' => array (size=0) empty protected '_error_messages' => array (size=0) empty protected '_error_prefix' => string '<p>' (length=3) protected '_error_suffix' => string '</p>' (length=4) protected 'error_string' => string '' (length=0) protected '_safe_form_data' => boolean false public 'validation_data' => array (size=0) empty 中放入一个var_dump:

$config = array(
    'radio_report' => array(
        array(
            'field' => 'id',
            'label' => 'id',
            'rules' => 'trim|required|is_natural_no_zero'
        )
    ),
    'join_request' => array(
        array(
            'field' => 'owner',
            'label' => 'owner',
            'rules' => 'trim|required|min_length[3]|max_length[100]|alpha'
        ),
        array(
            'field' => 'lang',
            'label' => 'lang',
            'rules' => 'trim|required|exact_length[2]|alpha'
        ),
        array(
            'field' => 'email',
            'label' => 'email',
            'rules' => 'trim|required|valid_email|max_length[60]'
        ),
        array(
            'field' => 'website',
            'label' => 'website',
            'rules' => 'trim|required|valid_url|max_length[255]'
        ),
        array(
            'field' => 'stream',
            'label' => 'stream',
            'rules' => 'trim|required|valid_url|max_length[255]'
        ),
        array(
            'field' => 'protocol',
            'label' => 'protocol',
            'rules' => 'trim|required|min_length[3]|max_length[30]'
        ),
        array(
            'field' => 'codec',
            'label' => 'codec',
            'rules' => 'trim|required|min_length[3]|max_length[10]'
        )
    )
);

这是我的验证文件(config / form_validation.php):

L_PREFER_CANVAS = true

3 个答案:

答案 0 :(得分:1)

根据Codeingniter文档,您可以扩展类

Loading Your Sub-class
To load your sub-class you’ll use the standard syntax normally used. DO NOT include your prefix. 

因此,请从自动加载

中删除'my_form_validation'

答案 1 :(得分:1)

如果是CI3,则无需为此类工作扩展课程。该方法包含在本机库中并可以使用:

$data['error_array'] = $this->form_validation->error_array();

CI v3中系统Form_validation库的第359行。

答案 2 :(得分:0)

我从子验证类中删除了构造,并为我工作!。

class MY_Form_validation extends CI_Form_validation {

    public function get_error_array(){
        return $this->_error_array;
    }
}
相关问题