Issues

ZF-6796: Zend_Form_Element_File - unable to create a Custom ErrorMessage

Description

I think a Code explains it very well. I want to Add Custom Error Messages in a Zend_Form_Element_File-Field. In a Zend_Form_Element_Text this is no Problem and works well with $form->txtelementname->addError( 'custom message' )

But it doesn't work with Zend_Form_Element_File. Here's a example code (copied from my controller)

I needed this because i want to check the filemime with my function. (Please ask not why because i think that's irrelevant)

 
<?php

// inner action function

require_once( FORM_PATH . 'AdminDownloadUpload.php' );
$form = new Frm_AdminDownloadUpload();

if( $this->getRequest()->isPost() ) {
            
            $fileValid = false;
            if( $form->file->isValid( $this->getRequest()->getPost() ) ) {
               if( !$form->file->receive() ) {
                    $form->file->addError( 'admin.download.lblFileUploadFailed' );
                }

                if( !$form->isErrors() && $form->file->isUploaded() && $form->file->isReceived() ) {
                    // check for valid mime
                    $check = Frames::getInstance()->checkMime( null, $form->file->getFileName() );
                    if( !$check ) {
                        $form->file->addError( 'admin.download.lblFileUploadFailedMimeType' );
                        @unlink( $form->file->getFileName() );
                    } else {
                        $fileValid = true;
                    }
                }
            }
            if( $form->isValid( $this->getRequest()->getPost() ) ) {
                $post = $form->getValues();
                
                // works?!              
                var_dump( $post );
                die;
            } else {
                // delete file again because form isn't valid
                if( $fileValid ) {
                    $form->file->addError( 'admin.download.lblFileUploadFailedFormNotValid' );
                    if( file_exists( $form->file->getFileName() ) ) @unlink( $form->file->getFileName() );
                }
            }
        }

        $this->view->assign( 'form', $form );

?>

Comments

Are you sure you're on ZF 1.8 ? The actual unittests for this component cover also addError and show no problem. User-errors are also returned.

What your code does not explain is what you get returned as failure because I don't see that you are calling getErrors().

I use the Zend MVC. So i only do a <?= $form ?> in the Template. I don't use getErrors because Zend_Form does the Job for me?! (as you see in the Controller $form->isValid..)

On all other Fields I got a ErrorMessage (incl. CustomErrorMessages) right under the input-Field except for the File Element.

<?php echo $form->render(new Zend_View());?>

Ok, ok. It's all my fault.

I checked the Versions now and found out, that the Script uses the ZF installed on Server (which was 1.7.5) and not the ZF in my Library Path. It works as expected with Version 1.8.1 and I'm sorry for my mistake. :-/

Thanks anyway.

I've updated this issue to Version 1.7.5 not 1.8.1.

Closing as duplicate