Welcome Guest, Not a member yet? Register   Sign In
validate type="file"
#1

[eluser]Mitja B.[/eluser]
Is it possible validate file? I want to validate file that it can not be empty but not working. $rules['logo_photo'] = "required"; not woking in type="file" but only for input type="text" field.

I search for solution but i din not find it
#2

[eluser]webdezzo[/eluser]
You can't validate a file using the built in CI validation because it does not have access to the $_FILES array, you will need to do something like this:

Code:
// Validation Rules
$rules['someotherfield']= "required";

// Set
$this->validation->set_rules($rules);
            
// Validation Fields
$fields['someotherfield'] = 'Other Field';
        
// Set
$this->validation->set_fields($fields);

// Run Validation
if ($this->validation->run() == FALSE)
{
// Did not pass validation of fields other than file
$do_add = false;
}
else
{
// Passed all other validation, now lets validate the file
if(is_array($_FILES) && $_FILES['userfile']['name'] != '')
{
// I could handle my file upload here etc and then upon success of file upload
// set true
$do_add = true;
}
else
{
// No file detected, but its required so we dont want finish this
$do_add = false;
}
}

// Now we can finish running the rest of our stuff
if($do_add)
{
// Finish up - everything was ok
}
else
{
// Fail
}

This is all psuedo code, but ...
#3

[eluser]xwero[/eluser]
webdezzo you make a common mistake by validating the file in the true branch of the validation->run method. If you do this most of the times you have to copy code from the false branch which fattens your controller.

The correct way to check if a file is added by the user is to check for the upload error 4, see php documentation for more info. The $_FILES global is always an array and by only checking if the filename is present you disregard the possibility of other errors.

So these two things make the code something like this
Code:
if( ! $this->validation->run() || $_FILES['userfile']['error'] == 4)
{
    if($_FILES['userfile']['error'] == 4)
    {
        // add to error_string
    }
    
}
The one problem i have with this method is that the file error appears last but it's possible the file field is at the top of the form but because of the handling of the error_string in the validation library there is no other option.
#4

[eluser]Mitja[/eluser]
xwero how to write error then. With

$rules['file']= "required";

$fields['file'] = 'File';

$this->validation->file_error

not working
#5

[eluser]xwero[/eluser]
Code:
$this->validation->error_string .= $this->validation->_error_prefix.$this->lang->line('required_file').$this->validation->_error_suffix;
#6

[eluser]ontguy[/eluser]
this is how I've done it by extending the validation class, it could just be a callback function as well.

MY_Validation.php
Code:
function file_upload_check($file, $required)
{
  if ($_FILES['userfile']['error'] == 4 && $required == 'FALSE')
  {
    return TRUE;
  }

  //do the file upload
  if (!$this->CI->catalog_lib->upload_file())
  {
    $this->CI->validation->set_message('file_upload_check', $this->CI->upload->display_errors('', ''));
    return FALSE;    
  }
  else
  {
    return TRUE;
  }
}

set the validation rule
$rules['file_validate'] = 'file_upload_check[TRUE]'; //used to validate file upload

the view
Code:
<input type="hidden" name="file_validate" value="upload_check">
<input type="file" name="userfile" size="20" />
#7

[eluser]Mitja[/eluser]
Fatal error: Class 'MY_validation' not found in C:\HTTPSERVER\wwwroot\avtoodpad\system\libraries\Loader.php on line 873

is it ok if is not like class?
#8

[eluser]ontguy[/eluser]
Is MY_Validation.php in application/libraries? Is the Validation class extended properly?
#9

[eluser]megabyte[/eluser]
Validate the form first. Then when you call the opload method from the file upload class it will return errors. If there is no file uploaded it exits and throws a "no file" error.

No need to change the core, it all does it for you.
#10

[eluser]jawnknee[/eluser]
ontguy, I'm trying to figure out your solution, but for some reason it seems like the upload library isn't working at all. If I print_r($_FILES['myfile']) it will show $_FILES indeed was set, yet if I var_dump($this->CI->upload) there is no data.

In your code:
Code:
//do the file upload
  if (!$this->CI->catalog_lib->upload_file())

I have tried putting the upload_file() function in the model, controller, library, and nothing seems to work... any ideas what I'm doing wrong? Thx!




Theme © iAndrew 2016 - Forum software by © MyBB