![]() |
Extended validation to validate files - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Extended validation to validate files (/showthread.php?tid=6036) |
Extended validation to validate files - El Forum - 02-12-2008 [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 My_Validation.php: Code: <?php View: Code: <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"> 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! Extended validation to validate files - El Forum - 02-12-2008 [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"; 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. |