Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Validator File MimeType (IsImage & IsCompressed) #3764

Closed
wants to merge 2 commits into from
Closed

Validator File MimeType (IsImage & IsCompressed) #3764

wants to merge 2 commits into from

Conversation

mwillbanks
Copy link
Contributor

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 ghost assigned weierophinney Feb 15, 2013
weierophinney added a commit that referenced this pull request Feb 19, 2013
weierophinney added a commit to zendframework/zend-validator that referenced this pull request May 15, 2015
weierophinney added a commit to zendframework/zend-validator that referenced this pull request May 15, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants