title: PHP - Codeigniter Validation using json
body: <p>I have a fields and every field have a validation. all validation are working. </p>
  
  <p>But the problem is in my field that will upload file. Even though i already put an image file still it will validate that I must upload an image. </p>
  
  <p>im using sweetalert to show the validation errors. </p>
  
  <p><strong>MY CONTROLLER</strong></p>
  
  <pre><code>public function saveproducts(){
      $config['upload_path'] = 'uploads/products';   
      $config['allowed_types'] = 'jpg|png|jpeg';
      $config['max_size'] = '2048000';
      $config['overwrite'] = TRUE;
      //$config['file_name'] = $this-&gt;input-&gt;post('prod_name');
  
      $this-&gt;load-&gt;library('upload', $config);
  
      $this-&gt;form_validation-&gt;set_rules('userfile', 'Product Image','callback_rulesprodimage');
  
      if($this-&gt;form_validation-&gt;run()==FALSE){
          echo json_encode(validation_errors());
      }else{
  
          $products = array(
              'product_image' =&gt; $this-&gt;input-&gt;post('userfile'),
              'upload_data' =&gt; $this-&gt;upload-&gt;data(),
              );
  
          $this-&gt;CrudModel-&gt;insertproductdata($products);
  
          echo json_encode(1);
      }
  
  }
  
  public function rulesprodimage(){
      if (isset($_FILES['userfile']) &amp;&amp; !empty($_FILES['userfile']['name'])){
          if ($this-&gt;upload-&gt;do_upload('userfile')){
          return true;
          }else{
          $this-&gt;form_validation-&gt;set_message('rulesprodimage', $this-&gt;upload-&gt;display_errors());
          return false;
          }
      }else{
        // throw an error because nothing was uploaded
        $this-&gt;form_validation-&gt;set_message('rulesprodimage', "You must upload an image!");
        return false;
      }
  }
  </code></pre>
  
  <p><strong>VIEW</strong></p>
  
  <pre><code>&lt;form method="post" enctype="multipart/form-data" id="prod-submit"&gt;
  &lt;div class="form-group"&gt;
  &lt;label class="control-label"&gt;Product Image &lt;span id="required"&gt; * &lt;/span&gt;&lt;/label&gt;
  &lt;input type="file" name="userfile" class="form-control"&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>Javascript</strong></p>
  
  <pre><code>$(document).ready(function(){
  $('#prod-submit').on('submit',function(e) { 
  
  $.ajax({
      url: base_url+'adminpage/saveproducts/',
  
      data:$(this).serialize(),
      type:'POST',
      success:function(data){
  
        var result = JSON.parse(data); 
          if(result===1){ 
              console.log(result);
              document.location.href = base_url+"adminpage/products/"
          }
          else{
             swal({
                  type: 'error',
                  html: result,
  
                }).done();
          }
      },
      error:function(data){
      swal("Oops...", "Something went wrong :(", "error");
  
      }
    });
    e.preventDefault(); 
  });
  </code></pre>
  
  <p>});</p>