Welcome Guest, Not a member yet? Register   Sign In
Validation rules not working in a form with File upload part (using CI 'upload' class)
#1

[eluser]Unknown[/eluser]
Hello,

Im trying to implement CI's 'form_validation' class to validate certain fields in a form. Im able to implement these rules in a simple form (without file upload part), but the same validations are not working in a form with file upload part(using CI native 'upload' class).



Following is the code

Controller code



Code:
<?

class Multiuploadfile extends Controller {

    function Multiuploadfile() {
        parent::Controller();

    }

    function index()
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');

        $this->form_validation->set_rules('title','Title','required');

        if($this->form_validation->run() == FALSE)
        {
            $this->load->view('multifileuploadView');

        }
        else
        {
            $this->load->view('multiuploadSuccess');        
        }
    }


    function multiple_upload($upload_dir = 'uploads')

    {


        $CI =& get_instance();
        $files = array();

        if(empty($config))
        {
            $config['upload_path']   = realpath($upload_dir);
            $config['allowed_types'] = 'gif|jpg|jpeg|jpe|png';
            $config['max_size']      = '2048';
        }
        
        $CI->load->library('upload', $config);
        
        $errors = FALSE;
        
        foreach($_FILES as $key => $value)
        {            
            if( ! empty($value['name']))
            {
                if( ! $CI->upload->do_upload($key))
                {                                          
                    $data['upload_message'] = $CI->upload->display_errors(ERR_OPEN, ERR_CLOSE);
                    $CI->load->vars($data);
                        
                    $errors = TRUE;
                }
                else
                {
                    // Build a file array from all uploaded files
                    $files[] = $CI->upload->data();
                }
            }
        }
        
        // There was errors, we have to delete the uploaded files
        if($errors)
        {                    
            foreach($files as $key => $file)
            {
                @unlink($file['full_path']);    
            }                    
        }
        elseif(empty($files) AND empty($data['upload_message']))
        {
            $CI->lang->load('upload');
            $data['upload_message'] = $CI->lang->line('upload_no_file_selected');
    

            $CI->load->vars($data);
        }
        else
        {
            return $files;
        }
    }



     function vik_multiupload() {

        $this->load->database();
        $this->load->model('mproducts');        
        


        $path = $_SERVER["DOCUMENT_ROOT"].'/ciprojects/ibmcibasics/system/application/uploads/';    

        if( ! $files = $this->multiple_upload($path))
        {
            echo 'Something went wrong during upload';
        }
        else
        {
            echo 'Upload success !<br />';
            $br = $_POST['brand'];    echo "Brand:".$br."<BR>";    
            $success = 'yes';
                $this->mproducts->entry_insert();

            
            echo '<pre>';
            print_r($files);
            echo '</pre>';
        $this->load->view('multiuploadSuccess');
        }


        







    }

}



View Code


Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Multiple File Upload Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php echo validation_errors(); ?&gt;


&lt;?php echo form_open_multipart('multiuploadfile/vik_multiupload');?&gt;




<h5>Title</h5>

&lt;input type="text" name="title" value=""    /&gt;

<h5>Condition</h5>
&lt;input type="text" name="condition" value="" /&gt;  

    &lt;input type="file" name="userfile1"&gt;&lt;br>
    &lt;input type="file" name="userfile2"&gt;&lt;br>
    &lt;input type="file" name="userfile3"&gt;&lt;br>
    &lt;input type="file" name="userfile4"&gt;&lt;br>
    &lt;input type="submit" value="Upload Images"&gt;&lt;br>
&lt;/form&gt;
#2

[eluser]Cro_Crx[/eluser]
The validation isn't happening because your posting your form to the vik_multiupload function on the multiuploadfile controller. This function (vik_multiupload) doesn't contain any form validation rules.

The rules and logic need to be in the controller your posting to and not the one your posting from if that makes sense.\

To elaborate: to fix the problem you can either put the form validation rules and logic into the vik_multiupload function. OR instead post to the current URL and have the logic for the success inside the current function.

You can post to the current URL by using the current_url() function within the uri helper.

Code:
&lt;?php echo form_open_multipart(current_url());?&gt;

Just make sure you load the helper in your controller.
#3

[eluser]Unknown[/eluser]
Thanks your solution worked!


Following is the updated code for the benefit of other users

Code:
[b]Controller Code[/b]

&lt;?

class Multiuploadfile extends Controller {

    function Multiuploadfile() {
        parent::Controller();

    }

    function index()
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');

        $this->form_validation->set_rules('title','Title','trim|required|xss_clean');
        $this->form_validation->set_rules('description','Description','trim|required|xss_clean');
        $this->form_validation->set_rules('link','Link','trim|required|xss_clean');
        $this->form_validation->set_rules('image_link','Image Link','trim|required|xss_clean');
        $this->form_validation->set_rules('price','Price','trim|required|xss_clean');
        $this->form_validation->set_rules('weight','Weight','trim|required|xss_clean');


        if($this->form_validation->run() == FALSE)
        {
            $this->load->view('multifileuploadView', array('error' => ' ' ));

        }
        else
        {

                $this->vik_multiupload();

        }
    }


    function multiple_upload($upload_dir = 'uploads', $config = array())
    {


        $CI =& get_instance();
        $files = array();

        if(empty($config))
        {
            $config['upload_path']   = realpath($upload_dir);
            $config['allowed_types'] = 'gif|jpg|jpeg|jpe|png';
            $config['max_size']      = '2048';
        }
        
        $CI->load->library('upload', $config);
        
        $errors = FALSE;
        
        foreach($_FILES as $key => $value)
        {            
            if( ! empty($value['name']))
            {
                if( ! $CI->upload->do_upload($key))
                {                                          
                    $data['upload_message'] = $CI->upload->display_errors(ERR_OPEN, ERR_CLOSE);
         // ERR_OPEN and ERR_CLOSE are error delimiters defined in a config file
                    $CI->load->vars($data);
                        
                    $errors = TRUE;
                }
                else
                {
                    // Build a file array from all uploaded files
                    $files[] = $CI->upload->data();
                }
            }
        }
        
        // There was errors, we have to delete the uploaded files
        if($errors)
        {                    
            foreach($files as $key => $file)
            {
                @unlink($file['full_path']);    
            }                    
        }
        elseif(empty($files) AND empty($data['upload_message']))
        {
            $CI->lang->load('upload');
            $data['upload_message'] = ERR_OPEN.$CI->lang->line('upload_no_file_selected').ERR_CLOSE;
    

            $CI->load->vars($data);
        }
        else
        {
            return $files;
        }
    }



     function vik_multiupload() {

        $this->load->helper(array('form', 'url'));
        $this->load->database();
        $this->load->model('mproducts');        
        
        $path = $_SERVER["DOCUMENT_ROOT"].'/ciprojects/ibmcibasics/system/application/uploads/';    

        if( ! $files = $this->multiple_upload($path))
        {
            echo 'Something went wrong during upload';
        }
        else
        {
            echo 'Upload success !<br />';
            $br = $_POST['brand'];    echo "Brand:".$br."<BR>";    
            $success = 'yes';
                $this->mproducts->entry_insert();

            
            echo '<pre>';
            print_r($files);
            echo '</pre>';
        $this->load->view('multiuploadSuccess');
        }
    }
}


[b]
View Code[/b]



&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Multiple File Upload Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php echo validation_errors(); ?&gt;


&lt;?php echo form_open_multipart(current_url());?&gt;

<h5>Title</h5>
&lt;?php
&lt;input type="text" name="title" value="&lt;?php echo set_value('title');?&gt;"    /&gt;

.
.
.
.
.
<h5> Upload Files</h5>
    &lt;input type="file" name="userfile1"&gt;&lt;br>
    &lt;input type="file" name="userfile2"&gt;&lt;br>
    &lt;input type="file" name="userfile3"&gt;&lt;br>
    &lt;input type="file" name="userfile4"&gt;&lt;br>
    &lt;input type="submit" value="Upload Images"&gt;&lt;br>
&lt;/form&gt;
#4

[eluser]CroNiX[/eluser]
Why are you doing this in multiple_upload()? (and other places)
$CI =& get_instance(); //no need for this line
$CI->load->library('upload', $config);

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




Theme © iAndrew 2016 - Forum software by © MyBB