Files

File handling is a breeze.

Use the file driver (not `input|file`) ``` $form->add(‘upload’, ‘file’); ``` When you add a file field to a form, the form automatically adds the `enctype="multipart/form-data"` attribute.
A file field’s value is always `NULL` when it’s empty (as opposed to an empty array from `$_FILES`). Because of this, you use the regular `not_empty` validation rule for making the field required. You can use Kohana’s file validation rules for any other file field rules. ``` $form->add_rule([ ‘upload’ => [ [‘not_empty’], ['Upload::type', [':value', ['jpg','png','gif']]], ] ]); ``` When you run `Formo::load()`, Formo always looks for any file fields inside the $_FILES array without having to pass the array into `Formo::load()`. Thus, this will work correctly: ``` $form->add('thumbnail', 'file') ->rule([ 'thumbnail' => [ ['not_empty'], ['Upload::type', [':value', ['jpg','png','gif']]], ] ]); // Formo will load the 'thumbnail' field from $_FILES when you call Formo::load() if ($form->load()->validate()) { // File passed } ```
When a file has a value, it is an array from `$_FILES`. The value looks like this: ``` array(5) ( "name" => string(40) "Screen Shot 2012-09-20 at 9.44.28 PM.png" "type" => string(9) "image/png" "tmp_name" => string(26) "/private/var/tmp/phpB4cFSq" "error" => integer 0 "size" => integer 62393 ) ```
comments powered by Disqus