Welcome Guest, Not a member yet? Register   Sign In
Input = file validation on multipart form
#1

[eluser]Linderman[/eluser]
Hell o! Smile

So, the situation is like this:

View:
Code:
<?php echo form_open_multipart('admin/infopages/info'); ?>
    
    <label>Infopage Title:</label><br />
    &lt;input type="text" name="title" value="" /&gt;&lt;br />
    
    <label>Infopage Content:</label><br />
    &lt;textarea name="content"&gt;&lt;/textarea><br />
    
    <label>Infopage Image:</label><br />
    &lt;input type="file" name="thumbnail" value="" /&gt;&lt;br />
    
    <label>Meta Keywords:</label><br />    
    &lt;textarea name="meta_keywords"&gt;&lt;/textarea><br />
    
    <label>Meta Description:</label><br />
    &lt;textarea name="meta_description"&gt;&lt;/textarea><br />
    
    &lt;input type="submit" name="submit" value="Save" /&gt;


&lt;/form&gt;

i am using multipart form!

in controller i am trying to say that input type='file' name='thumbnail' is required
Controller:
Code:
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
        
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('thumbnail', 'Image', 'required');

f($this->form_validation->run() == FALSE){
    $data['errors'] = TRUE;
} else {
    $data['errors'] = FALSE;

        ... and here goes image uploading code (not important for this problem)...
}

The problem is when i CHOOSE image and click the submit button CI always says "The Image field is required." AND I AM SHURE ITS NOT EMPTY. Why is that?

When i use normal form NOT MULTIPART its ok , CI gives the message only when i really didnt choose a file to upload (normal). But when i use multipart form CI blows this message always no matter the field is empty or not ...

Please, Help guys!
#2

[eluser]steelaz[/eluser]
I don't think you can validate file input with form_validation library, it checks $_POST array whereas uploaded file information is located in $_FILES array. You can use file upload library to check if file was uploaded or try JavaScript to validate it.
#3

[eluser]Aken[/eluser]
Create your own callback rule during forum validation, and use it to check the relevant file data.

http://ellislab.com/codeigniter/user-gui...#callbacks
#4

[eluser]$ilovephp[/eluser]
forget form validation class when you are only trying to say that a specific field is required if you are using multipart form. you can actually validate that using:

Code:
if($this->input->post('field_name')!=FALSE)
{
//set your error message and do not call the do_upload function
$error['error_message'] = "field_name is required.";
}
//
//
//
//and later in your code when you load the view
$this->load->view('view_filename',$error);

I opt to use this for one reason:
Why load an entire class (Form Validation Class) when you can do it without much codes?

I suggest you use Form Validation Class when you have multiple fields aside from that of multipart form
#5

[eluser]Linderman[/eluser]
Hello!

I validate only 1 fields because of the example. In my life project , the form is big and the file input is required. The question is not why i validate only 1 field using the corresponding class , the question is how to validate file input using normal way?

[quote author="steelaz" date="1272150655"]I don't think you can validate file input with form_validation library, it checks $_POST array whereas uploaded file information is located in $_FILES array. You can use file upload library to check if file was uploaded or try JavaScript to validate it.[/quote]

yes this is right

[quote author="Aken" date="1272246954"]Create your own callback rule during forum validation, and use it to check the relevant file data.

http://ellislab.com/codeigniter/user-gui...#callbacks[/quote]

and yes , this will do the thing Smile

Thank You guys for the answers, it helps a lot!
#6

[eluser]Unknown[/eluser]
Hello, here is my solution for file field validation :

In Form_validation.php file, add this in the function 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;
            }
            
            // Is there a validation rule for the particular URI being accessed?
            $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
            
            if ($uri != '' AND isset($this->_config_rules[$uri]))
            {
                $this->set_rules($this->_config_rules[$uri]);
            }
            else
            {
                $this->set_rules($this->_config_rules);
            }
    
            // We're we able to set the rules correctly?
            if (count($this->_field_data) == 0)
            {
                log_message('debug', "Unable to find validation rules");
                return FALSE;
            }
        }
    
        // Load the language file containing error messages
        $this->CI->lang->load('form_validation');
                            
        // Cycle through the rules for each field, match the
        // corresponding $_POST item and test for errors
        
        
        
        foreach ($this->_field_data as $field => $row)
        {
            // Fetch the data from the corresponding $_POST array and cache it in the _field_data array.
            // Depending on whether the field name is an array or a string will determine where we get it from.
            
            if ($row['is_array'] == TRUE)
            {
                $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']);
            }
            else
            {
                if ( (isset($_POST[$field]) AND $_POST[$field] != ""))
                {
                    $this->_field_data[$field]['postdata'] = $_POST[$field];
                }
                elseif (isset($_FILES[$field]) AND $_FILES[$field] != "")             //
                {                                                                     // Add this
                    $this->_field_data[$field]['postdata'] = $_FILES[$field]['name']; // Get the file name
                }                                                                     //
            }
        
            $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);        
        }

        // Did we end up with any errors?
        $total_errors = count($this->_error_array);

        if ($total_errors > 0)
        {
            $this->_safe_form_data = TRUE;
        }

        // Now we need to re-set the POST data with the new, processed data
        $this->_reset_post_array();
        
        // No errors, validation passes!
        if ($total_errors == 0)
        {
            return TRUE;
        }

        // Validation fails
        return FALSE;
    }

Then you can create your own rule, extending the form validation class, for exemple :

Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation {
    
    
    /*
     *
     * Validation on filename only
     *
     */
    
    function is_image($str)
    {
        if ($str == '')
        {
            return;
        }
        
        $a = explode(".", $_FILES['image']['name']);
        $a = array_reverse($a);
        $ext = strtolower($a[0]);
        
        $autorise = array('jpg', 'png', 'gif', 'jpeg');
        
        return in_array($ext, $autorise) ? TRUE : FALSE;
    }

}

It work good for me.

Bye




Theme © iAndrew 2016 - Forum software by © MyBB