CodeIgniter form validation errors display array format

CodeIgniter form validation errors display array format. validation_errors() function return errors in string format, For get it in array use error_array(). You will get detailed example with code for get form validation errors in array instead of string.

Codeigniter form_validation run method generate errors, by using validation_errors() method we able to get form field errors in string format. Result information comes with wrapped in paragraph tag and separate with new line.

Now how to get CodeIgniter form validation errors in specific formatting, like each error comes in array and field name as its key?

It’s simple, CodeIgniter form_validation library provide error_array() method for get all errors in specific array.

CodeIgniter form validation errors display array format

Code for get validation errors in array format


$this->load->library('form_validation');
$this->form_validation->set_message(...
$this->form_validation->set_rules(...

if ( $this->form_validation->run() == false ) {
    print_r( $this->form_validation->error_array() );
}

Hope you like solution, If you like CodeIgniter and want to learn useful tricks, you definitely like below post.

That’s it!