Extend form validation

2019-09-14 22:07发布

问题:

I need extend the Form Validation library, to make a method to return the error array.

I created a new library with the method 'get_error_array' within the library folder:

class My_Form_validation extends CI_Form_validation {

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

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

In the autoload config file i have:

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

But when i call $this->my_form_validation->get_error_array() in a controller after validate a submit, the method return an empty array.

Note: the validation was FALSE.

Any ideas ?

Update #1

According with the documentation i updated my code, but the result is the same:

class MY_Form_validation extends CI_Form_validation {

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

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

I disabled the autoload, and load manually the library from the controller:

   $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

UPDATE #2

I put an echo on the constructor and work. In the get_error_array put a 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

This is my validation file (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]'
        )
    )
);

回答1:

in accordance with Codeingniter documentation, whem you extends class

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

So, remove 'my_form_validation' from autoload



回答2:

If CI3, no need to extending class for such a job. That method is included in native library and ready to use:

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

Line 359 of system Form_validation library in CI v3.



回答3:

I removed the construct from the child validation class, and work for me !.

class MY_Form_validation extends CI_Form_validation {

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