Welcome Guest, Not a member yet? Register   Sign In
form_open_multipart with form_validation - am I missing something or is it a bug ?
#1

[eluser]DeaD SouL[/eluser]
Hello

This example doesn't want to work..
It keeps saying
Quote:The File field is required.
even if I removed the validation rule, it just show me the form again and again..

Am I missing something here ?

CI ver: 2.0.2

controller:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Test_files extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->library( 'form_validation' );
        $this->form_validation->set_rules( 'inp_file', 'File', 'required' );
        
        if( $this->form_validation->run() === FALSE )
        {
            $this->load->view( 'test_files' );
        }
        else
        {
            echo '<pre>', print_r( $_FILES, TRUE ), '</pre>';
        }
    }
}


view:
Code:
<!DOCTYPE html>
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="utf-8"&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?=form_open_multipart( $this->uri->uri_string() );?&gt;
    <div id="infoMessage">&lt;?=validation_errors();?&gt;</div>
    &lt;?=form_label('File: ', 'inp_file') . form_upload('inp_file', '', 'id="inp_file"');?&gt;
    &lt;?=form_submit('submit', 'Upload');?&gt;
&lt;?=form_close(); ?&gt;
&lt;/body&gt;
&lt;/html&gt;

Thanks
#2

[eluser]DeaD SouL[/eluser]
Anyone ??
#3

[eluser]davidbehler[/eluser]
You can't check for inputs with type="file" via the form validation library. It only validates what's in the $_POST array and file upload info will be in $_FILES.

Have a look at the file uploading class (http://ellislab.com/codeigniter/user-gui...ading.html) instead, maybe that'll help you.
#4

[eluser]DeaD SouL[/eluser]
You're right.. but as i said:

Quote:even if I removed the validation rule, it just show me the form again and again..

the upload class is great,.. but I just wonder why I can't use the form_validation library with &lt;input type="file" /&gt; ??

aw, thanks bro
#5

[eluser]davidbehler[/eluser]
To answer your first question: The test_files view is loaded everytime you submit the form because no validation rules = is not valid, meaning $this->form_validation->run() always returns FALSE.

To answer your second question: Because it doesn't support it Wink The lib doesn't know what type of input the fields are you set the rules for. It loops through all the rules and checks $_POST for each field according to what rules you defined, e.g. trim|required|valid_email. Now in case of an input with type="file", it'll not appear in $_POST but rather in $_FILES (but the form validation lib doesn't check that one).

Some time back I read about an extended version of the form validation that actually works with file uploads, but I haven't used it yet. I'm still kickin' it old school by combining form validation with file uploading and works fine for me so far.
#6

[eluser]DeaD SouL[/eluser]
Right,..

You made me check the form_validation::run()

Code:
[....]
    function run($group = '')
    {
        // Do we even have any data to process?  Mm?
        if (count($_POST) == 0)
        {
            return FALSE;
        }

        // Does the _field_data array containing the validation rules exist?
        // If not, we look to see if they were assigned via a config file
        if (count($this->_field_data) == 0)
        {
            // No validation rules?  We're done...
            if (count($this->_config_rules) == 0)
            {
                return FALSE;
            }
            [....]

Thanks waldmeister Smile




Theme © iAndrew 2016 - Forum software by © MyBB