5

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?

1 Answer 1

2

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 ?>
Sign up to request clarification or add additional context in comments.

1 Comment

The controller part does not have any effect, because $form->isValid() returns false. The view is an custom view and cannot be done via echo $this->form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.