View Single Post
  #1 (permalink)  
Old 03-31-2008, 09:50 AM
MiK MiK is offline
Junior Member
 
Join Date: Mar 2008
Location: Ancona, Italy
Posts: 17
Send a message via MSN to MiK
Default File uploads using Zend_Form

Hello all ,
i'm trying to create a form with file upload using the Zend_Form component using ZF version 1.5.1.

I created the Zend_Form_Element_File component which, for some reason, was missing (while Zend_Form_Element_File exists...)

Here's my Zend/Form/Element/File.php :
PHP Code:
require_once 'Zend/Form/Element/Xhtml.php';

class 
Zend_Form_Element_File extends Zend_Form_Element_Xhtml
{
    public 
$helper 'formFile';

And this is my controller action:
PHP Code:
function testAction()
{
     
$form = new Zend_Form();
     
$form->setAction('/this/test')
          ->
setMethod('post');

     
$file $form->createElement('file''file');

     
$form->addElement($file)
          ->
addElement('submit''upload', array('label' => 'Upload File'));

     
// Just to test if the file has really been processed...
     
if ($this->getRequest()->isPost())
     {
        
Zend_Debug::dump($_FILES);
        exit;
     }

    
$this->view->form $form;
    echo 
$this->view->render('testform.tpl');

This code will produce a small form with only one file upload field and the submit button, so it's really simple.

My problem is that i can't figure out some things:

1. How to set custom form enctype?
To handle file uploads i need to set it to 'multipart/form-data'. But i can't find anything to do such thing.. The only thing i know is that i should set some option(s) on the form decorator.

2. How to let Zend_Form to see file fields during form validation?
As seen on the ZF programming reference manual, the form is validated using:

PHP Code:
if ($form->isValid($_POST)) {
    
// success!
} else {
    
// failure!

Since php handles files upload using $_FILES superglobal array, maybe i could make some (dirty) thing like:

PHP Code:
$myPost array_merge($_POST$_FILES);
if (
$form->isValid($myPost)) {
   
// Processing form data ...

Could this be the quick&dirty solution ??

3. How to write validators for file fields ?
Maybe the method described in this post could be suitable for my needs..?


I tried to explain the thing as best as i could.
I'm not a native english-speaker, so please forgive me if i misspelled something.

Any help is appreciated.
Thank you all.
Reply With Quote