[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>
<input type="text" name="username" value="<?=$this->validation->username;?>" size="50" />
<h5>Password</h5>
<input type="text" name="password" value="<?=$this->validation->password;?>" size="50" />
<h5>Password Confirm</h5>
<input type="text" name="passconf" value="<?=$this->validation->passconf;?>" size="50" />
<h5>Email Address</h5>
<input type="text" name="email" value="<?=$this->validation->email;?>" size="50" />
<input type="text" name="myfile">
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
With the input type as text:
Code:
<input type="text" name="myfile">
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:
<input type="file" name="myfile">
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:
<input type="file" name="myfile">
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!