Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-31-2008, 09:50 AM
MiK MiK is offline
Junior Member
 
Join Date: Mar 2008
Location: Ancona, Italy
Posts: 13
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-31-2008, 11:21 AM
MiK MiK is offline
Junior Member
 
Join Date: Mar 2008
Location: Ancona, Italy
Posts: 13
Send a message via MSN to MiK
Default

Quote:
Originally Posted by MiK View Post
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.
Whoops i didn't noticed the Zend_Form::setAttrib() method!

PHP Code:
$form = new Zend_Form();
$form->setAttrib('enctype''multipart/form-data'); 
This has done the trick
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-06-2008, 09:43 AM
MiK MiK is offline
Junior Member
 
Join Date: Mar 2008
Location: Ancona, Italy
Posts: 13
Send a message via MSN to MiK
Default Something found

Check out this: Akra’s DevNotes Blog Archive Simple Zend_Form File Upload Example
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 10:14 AM.