title: CodeIgniter - form validation with image upload
body: <p>I have a form which accepts some text input fields and a file field (used to upload image).</p>
  
  <p>My problem is like that, if I did not fill one of the required fields and I have selected a valid image file, I will get an error message about the missing field, but the image will be uploaded. The Controller is:</p>
  
  <pre><code>    defined('BASEPATH') OR exit('No direct script access allowed');
  
      class Manage extends MY_Controller
      {
          public function __construct()
          {
              parent::__construct();
          }
  
          public function index()
          {
              $config = array(
                  array(
                      'field' =&gt; 'item_name',
                      'label' =&gt; 'Item name',
                      'rules' =&gt; 'required'
                  ),
                  array(
                      'field' =&gt; 'item_code',
                      'label' =&gt; 'Item code',
                      'rules' =&gt; 'required|is_unique[_items.item_code]'
                  ),
                  array(
                      'field' =&gt; 'item_description',
                      'label' =&gt; 'Item description',
                      'rules' =&gt; 'required'
                  ),
                  array(
                      'field' =&gt; 'item_img',
                      'label' =&gt; 'Item image',
                      'rules' =&gt; 'callback_upload_image'
                  )
              );
  
              $this-&gt;form_validation-&gt;set_rules($config);
              $this-&gt;form_validation-&gt;set_message('is_unique', 'Item code (' . $this-&gt;input-&gt;post('item_code') . ') already exists.');
  
              if($this-&gt;form_validation-&gt;run() == FALSE)
              {
                  // Render the entry form again
              }
              else
              {
                  // Go to another page
              }
          }
  
          public function upload_image()
          {
              $config['upload_path'] = './items_images';
              $config['max_size'] = 1024 * 10;
              $config['allowed_types'] = 'gif|png|jpg|jpeg';
              $config['encrypt_name'] = TRUE;
  
              $this-&gt;load-&gt;library('upload', $config);
  
              if(isset($_FILES['item_img']) &amp;&amp; !empty($_FILES['item_img']['name']))
              {
                  if($this-&gt;upload-&gt;do_upload('item_img'))
                  {
                      $upload_data = $this-&gt;upload-&gt;data();
                      $_POST['item_img'] = $upload_data['file_name'];
                      return TRUE;
                  }
                  else
                  {
                      $this-&gt;form_validation-&gt;set_message('upload_image', $this-&gt;upload-&gt;display_errors());
                      return FALSE;
                  }
              }
              else
              {
                  $_POST['item_img'] = NULL;
                  return FALSE;
              }
          }
      }
  </code></pre>
  
  <p>As I know, if one rule failed, other fields and operations will be canceled and the form will be loaded again, or other operations will be done.</p>
  
  <p>Thank you,,,</p>