【问题标题】:Extend form validation扩展表单验证
【发布时间】:2015-07-19 09:34:15
【问题描述】:

我需要扩展表单验证库,以创建一个返回错误数组的方法。

我在库文件夹中使用方法“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;
    }
}

在我的自动加载配置文件中:

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

但是当我在验证提交后在控制器中调用$this->my_form_validation->get_error_array() 时,该方法返回一个空数组。

注意:验证为 FALSE。

有什么想法吗?

更新 #1

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

class MY_Form_validation extends CI_Form_validation {

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

    public function get_error_array(){
        return $this->_error_array;
    }
}

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

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

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

更新 #2

我在构造函数上放了一个回声并开始工作。在get_error_array 中放一个var_dump:

# 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

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

$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]'
        )
    )
);

【问题讨论】:

  • 当你扩展类时,你仍然像往常一样使用它。无需自动加载“my_form_validation”库,当您想访问“get_error_array()”方法时,只需使用它; $this->form_validation->get_error_array();
  • @ramiromd 您的代码适用于我的 ci 给出的错误数组。将 echo 添加到 costructor 以确保您的扩展类已加载
  • 你确定Note: the validation was FALSE. 吗?只是我看看它是如何工作的 :) 所以,你的代码没有错误

标签: php codeigniter validation


【解决方案1】:

如果是CI3,不需要为这样的工作扩展类。该方法包含在本机库中并可以使用:

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

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

【讨论】:

  • 优秀。我之前没有看到 error_array() 方法。
【解决方案2】:

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

class MY_Form_validation extends CI_Form_validation {

    public function get_error_array(){
        return $this->_error_array;
    }
}

【讨论】:

    【解决方案3】:

    根据 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'

    【讨论】:

      猜你喜欢
      • 2012-02-27
      • 1970-01-01
      • 2015-01-20
      • 2013-09-24
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多