Welcome Guest, Not a member yet? Register   Sign In
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!


Messages In This Thread
Extended validation to validate files - by El Forum - 02-12-2008, 08:48 PM
Extended validation to validate files - by El Forum - 02-12-2008, 09:54 PM



Theme © iAndrew 2016 - Forum software by © MyBB