0

I have a form that allows user to upload multiple files. Additionaly user have to fill other input text fields like address, email etc.

I don't know how to make form sticky if error comes from file uploading and how to access error messages set by form_validation library. Because if for example one of the filetype is not allowed I use redirect to step out of the loop responsible for uploading every file.

To keep upload error message I use $this->session->set_flashdata('errors', $errors) but then I'm not able to use error messages from form_validation library ( echo form_error('field_name) ) and display user input in form fields ( echo set_value('field_name'))

Here is controller method responsible for file upload and data insert:

function do_upload()
{
    // load form validation library
    $this->load->library('form_validation');

    // set validation rules        
    $this->form_validation->set_rules('email','Email','trim|required|valid_email|matches[confirm_email]');
    $this->form_validation->set_rules('confirm_email','Email','trim|required|valid_email');
    $this->form_validation->set_rules('delivery_name','Nazwa','trim|required');
    $this->form_validation->set_rules('delivery_street','Ulica','trim|required');
    $this->form_validation->set_rules('delivery_city','Miasto','trim|required');
    $this->form_validation->set_rules('delivery_postcode','Kod pocztowy','trim|required');
    $this->form_validation->set_rules('delivery_country','Kraj','trim|required');
    $this->form_validation->set_rules('captcha','Captcha','callback_check_captcha');

    if ( $this->form_validation->run() )
    {
        // if validation passes I insert form data into database and I start to upload
        // all the files using foreach loop


        $config['upload_path'] = './uploads/upload_directory';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '0';

        $upload_data = array();
        $task_address_id_array = $this->MTasks->insertNewTask($task_directory);

        $task_id = $task_address_id_array['task_id'];
        $address_id = $task_address_id_array['address_id'];

        // iterate over files to be uploaded
        foreach ($_FILES as $key => $value)
        {

            if (!empty($value['name']))
            {
                $this->upload->initialize($config);
                $result = $this->upload->do_upload($key);
                if (!$result)
                {
                    // if upload error occurs I need to redirect to break the loop
                    $errors = $this->upload->display_errors();
                    $this->session->set_flashdata('errors', $errors);

                    redirect();

                }
                else
                {
                    $current_upload_data = $this->upload->data();
                    // insert upload data to database
                    $this->MTasks->insertFileInfo($current_upload_data,$task_id);
                }

            }
        }
        redirect('upload/success_message');
    }
    else
    {
        $this->load->view('include/header');
        $this->load->view('upload_form',$this->data);
        $this->load->view('include/footer');
    }


}

1 Answer 1

0

The main problem is you can not put as validation rule file uploads, because the library executes one by one.

A simple solution is to validate your fields and then validate the file upload. Example:

<?php

function save()
{
    $this->load->library('form_validation');
    $this->form_validation->set_rules('name', 'Name', 'trim|required');

    if($this->form_validation->run() == FALSE)
    {
        $this->form_validation->set_error_delimiters('', '<br />');
        echo validation_errors();
        exit();
    }
    else
    {
        if($_FILES['imagen']['error'] != 4)
        {
            $image $this->_manage_image($this->input->post('name'));
        }
        else
        {
            exit('No empty field!');
        }

        # save in your bd o success message...  
    }
}

function _manage_image($name) 
{
    # config
    $config['upload_path'] = './images/products/';
    $config['allowed_types'] = 'gif|jpg|png';
    // ...
    $config['file_name'] = $name;

    $this->load->library('upload', $config);

    if (!$this->upload->do_upload('image'))
    {
        # errors...
        echo $this->upload->display_errors();
        exit();
    }
    else
    {
        # upload
        return $img_data['file_name'];  
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.