• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extended validation to validate files

#1
[eluser]webdezzo[/eluser]
Ok all, in experimenting I have come across something I found interesting. Here goes. When having the code below, I can come close to validating files (just the file extension say) from the $_FILES array. But it's haggard that I cannot call the actual validation on anything that is an input type of a 'file'.

Controller:
Code:
<?php

class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();    
    }
    
    
    function index()
    {
        $this->load->library('validation');
        $this->load->helper(array('form', 'url'));
        
        $rules['username']    = "required";
        $rules['password']    = "required";
        $rules['passconf']    = "required";
        $rules['email']        = "required";
        $rules['myfile']    = "required|checkme";
        
        $this->validation->set_rules($rules);
        
        $fields['username']    = 'Username';
        $fields['password']    = 'Password';
        $fields['passconf']    = 'Password Confirmation';
        $fields['email']    = 'Email Address';
        $fields['myfile']    = 'File';
    
        $this->validation->set_fields($fields);
            
        if ($this->validation->run() == FALSE)
        {
            $this->load->view('testform');
        }
        else
        {
            $this->load->view('formsuccess');
        }
    }
    
}
?>

My_Validation.php:
Code:
<?php

class MY_Validation extends CI_Validation{
    
    /*
    ------------------------------
    Extend Validation Library
    ------------------------------
    */
    
    function MY_Validation(){
        parent::CI_Validation();
    }
    
    
    function checkme(){
        
        echo "I AM CALLING VALLY";
        
        print_r($_FILES);
        
        $CI =& get_instance();
        $CI->validation->set_message('checkme', 'The %s field is invalid');
        
        return true;
        
    }
    
    
    
}
    
?>

View:
Code:
<html>
<head>
<title>My Form</title>
</head>
<body>

<?=$this->validation->error_string; ?>

<?=form_open_multipart('welcome/testery'); ?>

<h5>Username</h5>
&lt;input type="text" name="username" value="&lt;?=$this-&gt;validation->username;?&gt;" size="50" />

<h5>Password</h5>
&lt;input type="text" name="password" value="&lt;?=$this-&gt;validation->password;?&gt;" size="50" />

<h5>Password Confirm</h5>
&lt;input type="text" name="passconf" value="&lt;?=$this-&gt;validation->passconf;?&gt;" size="50" />

<h5>Email Address</h5>
&lt;input type="text" name="email" value="&lt;?=$this-&gt;validation->email;?&gt;" size="50" />

&lt;input type="text" name="myfile"&gt;

<div>&lt;input type="submit" value="Submit" /&gt;</div>

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;

With the input type as text:
Code:
&lt;input type="text" name="myfile"&gt;

and the validation in the controller placed as below:
Code:
$rules['myfile']    = "required|checkme";

I can successfully echo out the $_FILES array even though there is nothing in it
and hit the extended validation.

However, if I use the following:
Code:
&lt;input type="file" name="myfile"&gt;
with the same controller validation:
Code:
$rules['myfile']    = "required|checkme";

It will no longer hit the extended validation.

But the plot thickens...

If I keep the file type input
Code:
&lt;input type="file" name="myfile"&gt;

And move the |checkme call from the 'myfile' rule, and place it in the 'username' rules like such:
Code:
$rules['username'] = "required|checkme";

It will also fire the validation and at that point echo out the $_FILES array from the file that I posted.

It would be really nice to write custom validation this easily for files. Am I missing something?

Thanks in advance!

#2
[eluser]tonanbarbarian[/eluser]
The reason the checkme validation is not being run when the input type is file is because there is not a POST variable called myfile.
Instead that data is in the _FILES.
The issue is that the rule says required, and once it determines that the field does not exist in POST then it will stop processing and raise an error indicating that the field is required.
You could try swapping the order of the rules
Code:
$rules['myfile']    = "checkme|required";
but i think the required check is done independantly so it might not work, and then even if the checkme passed it would fail the required and produce a validation error

i think there are some people working on modifications to the validation library to allow it to process the files as well as the post vars.

I would extend the run method of the validation code to copy the _FILES data into the POST array so that the validation rules have something to look at.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.