Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Validator File MimeType (IsImage & IsCompressed) #3764

Closed
wants to merge 2 commits into from

2 participants

@mwillbanks
Collaborator

Overview

The validators for items that extend MimeType take several options; the design of which causes some potential issues. This design is still left intact but should be looked at for future versions.

I came over a bug where the default mimetypes were not set when using input filters. When you utilize one of these as an input filter and pass in custom messages or set any other option the default mimetype options will not be set due to a check of empty in the constructor. Additionally the parent constructor does a lot of magic to determine if there is indeed a mimetype that can be applied.

The change now allows the parent constructor to set any mimetypes if it can find any and then check to see if any mimetypes have been set; if not it will set the default mimetypes.

Example that caused the pain

This is a quick example; I have some additional logic that takes some of the fields and automatically creates labels and such so don't pick apart the element too much here... Note that due to the messages section of File\IsImage on the validator causes the default mime types to not be leveraged here.

namespace MyModule\Form;

use Zend\Form\Form;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;

ImageUpload extends Form
{
    public function __construct()
    {
        parent::__construct();
        $this->add(array(
            'name' => 'image',
            'attributes' => array(
                'type' => 'file',
                'required' => 'required',
            ),  
        )); 
        $this->add(array(
            'name' => 'upload',
            'attributes' => array(
                'class' => 'button',
                'type' => 'submit',
                'required' => 'required',
            ),  
        )); 
    }

    public function getInputFilter()
    {   
        if (!$this->filter) {
            $this->filter = new InputFilter();
            $factory = new InputFactory();
            $this->filter->add($factory->createInput(array(
                'name' => 'image',
                'required' => true,
                'validators' => array(
                    array(
                        'name' => 'NotEmpty',
                        'options' => array(
                            'messages' => array(
                                'isEmpty' => 'Please select an icon to upload.',
                            ),
                        ),
                    ),
                    array(
                        'name' => 'File\IsImage',
                        'options' => array(
                            'messages' => array(
                                'fileIsImageFalseType' => 'Please select a valid icon image to upload.',
                                'fileIsImageNotDetected' => 'The icon image is missing mime encoding, please verify you have saved the image with mime encoding.',
                            ),
                        ),
                    ),
                ),
            )));
        }
        return parent::getInputFilter();
    }
}
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/3764'
Close #3764
939bb84
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/3764' into develop
Forward port #3764
8d28dad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
8 library/Zend/Validator/File/IsCompressed.php
@@ -79,10 +79,14 @@ public function __construct($options = array())
$options = ArrayUtils::iteratorToArray($options);
}
- if (empty($options)) {
- $options = array('mimeType' => $default);
+ if ($options === null) {
+ $options = array();
}
parent::__construct($options);
+
+ if (!$this->getMimeType()) {
+ $this->setMimeType($default);
+ }
}
}
View
8 library/Zend/Validator/File/IsImage.php
@@ -104,10 +104,14 @@ public function __construct($options = array())
$options = ArrayUtils::iteratorToArray($options);
}
- if (empty($options)) {
- $options = array('mimeType' => $default);
+ if ($options === null) {
+ $options = array();
}
parent::__construct($options);
+
+ if (!$this->getMimeType()) {
+ $this->setMimeType($default);
+ }
}
}
View
9 tests/ZendTest/Validator/File/IsCompressedTest.php
@@ -182,6 +182,15 @@ public function testOptionsAtConstructor()
$this->assertEquals('image/gif,image/jpg', $validator->getMimeType());
}
+ public function testNonMimeOptionsAtConstructorStillSetsDefaults()
+ {
+ $validator = new File\IsCompressed(array(
+ 'enableHeaderCheck' => true,
+ ));
+
+ $this->assertNotEmpty($validator->getMimeType());
+ }
+
/**
* @group ZF-11258
*/
View
9 tests/ZendTest/Validator/File/IsImageTest.php
@@ -173,6 +173,15 @@ public function testOptionsAtConstructor()
$this->assertEquals('image/gif,image/jpg', $validator->getMimeType());
}
+ public function testNonMimeOptionsAtConstructorStillSetsDefaults()
+ {
+ $validator = new File\IsImage(array(
+ 'enableHeaderCheck' => true,
+ ));
+
+ $this->assertNotEmpty($validator->getMimeType());
+ }
+
/**
* @group ZF-11258
*/
Something went wrong with that request. Please try again.