Welcome Guest, Not a member yet? Register   Sign In
form validation with image upload
#1

Greetings all,
I have a form which accepts some text input fields and a file field (used to upload image).
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:

PHP 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' => 'item_name',
 
                   'label' => 'Item name',
 
                   'rules' => 'required'
 
               ),
 
               array(
 
                   'field' => 'item_code',
 
                   'label' => 'Item code',
 
                   'rules' => 'required|is_unique[_items.item_code]'
 
               ),
 
               array(
 
                   'field' => 'item_description',
 
                   'label' => 'Item description',
 
                   'rules' => 'required'
 
               ),
 
               array(
 
                   'field' => 'item_img',
 
                   'label' => 'Item image',
 
                   'rules' => 'callback_upload_image'
 
               )
 
           );

 
           $this->form_validation->set_rules($config);
 
           $this->form_validation->set_message('is_unique''Item code (' $this->input->post('item_code') . ') already exists.');

 
           if($this->form_validation->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->load->library('upload'$config);

 
           if(isset($_FILES['item_img']) && !empty($_FILES['item_img']['name']))
 
           {
 
               if($this->upload->do_upload('item_img'))
 
               {
 
                   $upload_data $this->upload->data();
 
                   $_POST['item_img'] = $upload_data['file_name'];
 
                   return TRUE;
 
               }
 
               else
                
{
 
                   $this->form_validation->set_message('upload_image'$this->upload->display_errors());
 
                   return FALSE;
 
               }
 
           }
 
           else
            
{
 
               $_POST['item_img'] = NULL;
 
               return FALSE;
 
           }
 
       }
 
   

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.

Thank you,,,
Reply
#2

I think it is because one of your rules is a callback, and the callback function is doing the upload. The callback will always be called so the upload is always being done.

You should only do the upload once all the error checking is done, not as part of the error checking.

I have not done a file upload for some time but I would check all the fields for errors first, then if they pass the error checking, do a an attempt at the file upload. If that fails you can repopulate your form and send a message about the upload error.

I hope I have understood your code properly and that this is of some help,

Best wishes,

Paul.
Reply
#3

Greetings PaulD,
Thank you for your response. Actually I did same way that you said. I have checked the fields first, then after they are OK, I uploaded the picture. I think I understand something, that callback only useful in form validation if you are validating only one input.

Regards,,,
Reply




Theme © iAndrew 2016 - Forum software by © MyBB