Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Form Validator File Upload goes invalid

I got and Zend Form which does this:

 class Products_AddForm extends Zend_Form {
  public function init() {

    $ProductImage1 = $mainform->addElement('file', 'Productimage1',     array(
                               'validators' => array(
                                    array('Count', false, '1' ),
                                    array('Size', false, '10MB'),
                                    array('Extension', false, 'jpg,jpeg,tif,eps'),
                                ),
                                'required'   => false,
                                'label' => 'Product Image1 (jpg/tif/eps)'
            ));

Then an Controller which checks the post data:

public function addAction()
{

    $form = $this->getAddForm();

    if($this->getRequest()->isPost()){

        $post =  $this->getRequest()->getPost();

        // check post data
        if($form->isValid($post) )
        { 
        }
        else {
            print_r($form->getErrors());
            print_r($form->getErrorMessages());
            print_r($form->getMessages());
        }

And a custom view controller something like this:

echo '<form method="post" action="'.$this->baseUrl('products/add').'" enctype="multipart/form-data">';

            $image1 = $form->getElement('Productimage1');

            $helper1 = $image1->helper;
            echo '<br/>'.$this->translate('Productimage').' (jpg/tif):<br/>'.$bild1->getView()->$helper1(
            $image1->getName(),
            $image1->getValue(),
            $image1->getAttribs(),
            $image1->options
            );

When I just type some information in textfields and post it, $form->isValid() goes false. When I delete the file parts from the form and the custom view, it works perfectly.

Formelement is on 'required' false. In addition the false part of the controller -> getErrors(), getMessages() and getErrorMessages() returns no errors?!

Does anybody knows whats going on here?

like image 366
frgtv10 Avatar asked Nov 03 '22 21:11

frgtv10


1 Answers

Try it like this: This is your form:

   $image = $this->createElement('file','image');
            $image->setLabel('Product Image1 (jpg/tif/eps) ')
                  ->setRequired(false)
                  ->setDestination(??)
                  ->addValidator('Count',false,1)
                  ->addValidator('Size',false,10MB)
                  ->addValidator('Extension',false,'jpg,tif,eps');
            $this->addElement($image);

This your controller:

$form = new Application_Form_YOURFORM(array('action' => '', 'method' => 'POST'));
        if($this->_request->isPost() && $form->isValid($this->_request->getPost()))
        {
            if($form->image->isUploaded())
            {
                $form->image->receive();
                $this->image = 'destination' . basename($form->image->getFileName());
            }
            $this->_helper->redirector('..');
        }
        else
        {
            $this->view->form = $form;
        }

Your view:

<?php echo $this->form ?>
like image 180
Daan Avatar answered Nov 08 '22 08:11

Daan