Seamlessly validate forms and file uploads with CodeIgniter
The Form Validation class is great to validate simple input fields such as textareas, checkboxes, text fields, etc. But what about file inputs? If you have a mix of text and file inputs, your controller can get messy if you try to validate both. Lets take a look at a neat solution that only involves extending your Form Validation class.
First of all we must use CodeIgniter’s Form Validation class and extend it. This extension will allow you to save custom errors to the error_array and then print it out as if it was an standard text field.
Create a new file named ‘MY_Form_validation.php’ and save it to ‘application/libraries’. Add the following code to this file:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class MY_Form_validation extends CI_Form_validation { /** * Add error * * Adds a custom error to the form validation array * * @return array */ function add_to_error_array($field = '', $message = '') { if ( ! isset($this->_error_array[$field])) { $this->_error_array[$field] = $message; } return; } /** * Error Array * * Returns the error messages as an array * * @return array */ function error_array() { if (count($this->_error_array) === 0) return FALSE; else return $this->_error_array; } } /* End of file MY_Form_validation.php */ /* Location: ./application/libraries/MY_Form_validation.php */
Now in order to validate your mixed form you can have your controller do some logic like this:
<?php class Add_profile extends CI_Controller { public function index() { $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email'); $this->form_validation->set_rules('fullname', 'Fullname', 'trim|required'); if ($this->form_validation->run($this)) { // All good, now proceed to file validation if($_FILES['profile_image']['size'] > 0) { $config['upload_path'] = 'assets/profiles/'; $config['allowed_types'] = 'jpg|jpeg|png'; $config['max_size'] = '1024'; $this->upload->initialize($config); if ($this->upload->do_upload('profile_image')) { $profile_data = $this->upload->data(); // $profile_data['file_name'] contains the filename // in case you need to save it into a database } else { // Here comes the magic, we just add the file upload error to the Form Validation array $this->form_validation->add_to_error_array('profile_image', $this->upload->display_errors('', '')); } } } // Validate if the error array has any items if(!$this->form_validation->error_array()) { // NO errors on! load a view or continue... } } }
You can also use the helper validation_errors() in your view to output all the errors including any file upload issues that may have happened. You can always play with the logic and adapt it to your application needs.