Task #37063

Re-Evaluate the best practice usage of the Image Validator "network"

Added by Adrian Föder about 3 years ago. Updated almost 3 years ago.

Status:Resolved Start date:2012-05-10
Priority:Should have Due date:
Assigned To:- % Done:

0%

Category:-
Target version:-

Description

consider the following use case

1    /**
2     * @param \TYPO3\Media\Domain\Model\Image $newImage
3     * @FLOW3\Validate("$newImage", type="\TYPO3\Media\Validator\ImageSizeValidator", options={ "minimumWidth"=150, "minimumHeight"=150 })
4     * @FLOW3\Validate("$newImage", type="\TYPO3\Media\Validator\ImageTypeValidator", options={ "allowedTypes"={"jpeg","png","gif"} })
5     * @return void
6     */
7    public function createAction(\TYPO3\Media\Domain\Model\Image $newImage) {
8    }

---
1 <f:form action="create" name="newImage" enctype="multipart/form-data"> 2 <f:form.upload property="resource" class="upload" /> 3 </f:form>

Currently, providing a non-image file like a .txt file, results in three validation errors:

  • 1319801362: The actual image width of 0 is lower than the allowed minimum width of 150.
  • 1319801925: The actual image height of 0 is lower than the allowed minimum height of 150.
  • 1328030664: The uploaded file is no valid image.

The first two messages are of course thrown in the SizeValidator, the last message is from the ImageTypeValidator.
Interestingly, the getimagesize() method from the Image constructor does not result in a warning or similar when providing a text file.

If providing no image at all (i.e. submitting an empty form), the following exception occurs:

#1: Catchable Fatal Error: Argument 1 passed to Acme\Foo\Controller\ImageController_Original::createAction() must be an instance of TYPO3\Media\Domain\Model\Image, null given

The latter occurence could be fetched with adding the NotEmptyValidator; but what to do in case of an invalid image? Generally the TypeValidator should be responsible for that; but due to the fact that an Image instance is generated however, the SizeValidator also comes to place.

Having talked to Christian about that, we came to the idea that the Image (constructor) should throw an exception if it's not actually an image (per PHP doc it should however throw a Warning, but I'll check that).
That exception could be caught by the ImageTypeConverter, returning NULL if that Exception occurs.
So at the end, again the NotEmptyValidator would catch that case.

I'll provide a patch for that; feel free to add comments here meanwhile.

Associated revisions

Revision 9f957b96
Added by Adrian Föder about 3 years ago

[TASK] Adjust behavior for the case of non-valid images

Errors and Exceptions that occur while trying to handle
an image are now caught smoothly.

The Image TypeConverter returns an error object if
such an Exception happens, making it possible to
display user friendly validation messages on upload.

Change-Id: I4897b74ec421b23ef667a0fbd7b65926866791cc
Resolves: #37063

History

#1 Updated by Adrian Föder about 3 years ago

  • Status changed from New to Under Review

#2 Updated by Gerrit Code Review about 3 years ago

Patch set 2 for branch master has been pushed to the review server.
It is available at http://review.typo3.org/11136

#3 Updated by Gerrit Code Review about 3 years ago

Patch set 3 for branch master has been pushed to the review server.
It is available at http://review.typo3.org/11136

#4 Updated by Adrian Föder almost 3 years ago

  • Status changed from Under Review to Resolved

has been merged some time ago.

Also available in: Atom PDF