Welcome Guest, Not a member yet? Register   Sign In
Extended validation to validate files

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'.


class Welcome extends Controller {

    function Welcome()
    function index()
        $this->load->helper(array('form', 'url'));
        $rules['username']    = "required";
        $rules['password']    = "required";
        $rules['passconf']    = "required";
        $rules['email']        = "required";
        $rules['myfile']    = "required|checkme";
        $fields['username']    = 'Username';
        $fields['password']    = 'Password';
        $fields['passconf']    = 'Password Confirmation';
        $fields['email']    = 'Email Address';
        $fields['myfile']    = 'File';
        if ($this->validation->run() == FALSE)


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

<title>My Form</title>

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

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

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

&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>



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

and the validation in the controller placed as below:
$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:
&lt;input type="file" name="myfile"&gt;
with the same controller validation:
$rules['myfile']    = "required|checkme";

It will no longer hit the extended validation.

But the plot thickens...

If I keep the file type input
&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:
$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!

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
$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.

Theme © iAndrew 2016 - Forum software by © MyBB